spread-syntax

State as array of objects vs object keyed by id

落爺英雄遲暮 提交于 2019-11-28 13:35:01
问题 In the chapter on Designing the State Shape, the docs suggest to keep your state in an object keyed by ID: Keep every entity in an object stored with an ID as a key, and use IDs to reference it from other entities, or lists. They go on to state Think of the app’s state as a database. I'm working on the state shape for a list of filters, some of which will be open (they're displayed in a popup), or have selected options. When I read "Think of the app’s state as a database," I thought about

Spreading undefined in array vs object

南楼画角 提交于 2019-11-28 09:41:42
Why does spreading undefined in an object return an empty object? {...undefined} // equals {} : console.log({...undefined}) And Why does spreading undefined in an array give you an error? [...undefined] // type error : console.log([...undefined]) As noted in the comments, and summarized by @ftor from #687 , object spread is equivalent 1 to Object.assign() (issues #687 , #45 ), where as spread in array literal context is iterable spread. Quoting Ecma-262 6.0 , Object.assign() is defined as: 19.1.2.1 Object.assign ( target, ...sources ) The assign function is used to copy the values of all of

Typescript recursive function composition

旧街凉风 提交于 2019-11-28 05:59:10
问题 I want to create a function chain, which would be an input of a pipe/flow/compose function. Is this possible without the literal expansion of the types to selected depth, as is this usually handled? See lodash's flow. I want to achieve typecheck of the data flow in the chain. - Argument of a function is result of the previous one - First argument is a template parameter - Last return is a template parameter type Chain<In, Out, Tmp1 = any, Tmp2 = any> = [] | [(arg: In) => Out] | [(arg: In) =>

How to add TypeScript types to destructured parameters using spread syntax?

江枫思渺然 提交于 2019-11-28 04:49:51
问题 Ignore the fact that this is bad add function. It's a question about using array destructuring with spread syntax in TypeScript. This is what I'm trying const add = ([x,...xs]) => { if (x === undefined) return 0 else return x + add(xs) } console.log(add([1,2,3])) //=> 6 But I have no idea how to add TypeScript types to this. My best guess is to do something like this (most direct translation) const add = (xs: number[]): number => { if (xs[0] === undefined) return 0; else return xs[0] + add(xs

Usage of rest parameter and spread operator in javascript

情到浓时终转凉″ 提交于 2019-11-27 19:29:26
What's the usage of rest parameter that will be added in ECMAScript 6? For example, in ECMAScript 5 you can do the following to get an array of parameters starting from the second element: // ES 5 store('Joe', 'money'); store('Jane', 'letters', 'certificates'); function store(name) { var items = [].slice.call(arguments, 1); //['money'] in first case items.forEach(function (item) { vault.customer[name].push(item); }); } and that will be equivalent to the following code in ECMAScript 6: // ES 6 store('Joe', 'money'); store('Jane', 'letters', 'certificates'); function store(name, ...items) {

Deep copy in ES6 using the spread syntax

。_饼干妹妹 提交于 2019-11-27 18:54:47
I am trying to create a deep copy map method for my Redux project that will work with objects rather than arrays. I read that in Redux each state should not change anything in the previous states. export const mapCopy = (object, callback) => { return Object.keys(object).reduce(function (output, key) { output[key] = callback.call(this, {...object[key]}); return output; }, {}); } It works: return mapCopy(state, e => { if (e.id === action.id) { e.title = 'new item'; } return e; }) However it does not deep copy inner items so I need to tweak it to: export const mapCopy = (object, callback) => {

PHP Spread Syntax in Array Declaration

蹲街弑〆低调 提交于 2019-11-27 17:18:40
问题 PHP supports the spread syntax for variadic functions. In JavaScript, you can use the spread syntax to do this: var a = [1, 2]; var b = [...a, 3, 4]; console.log(b); // [1, 2, 3, 4] However, trying to do this in PHP: $a = [1, 2]; $b = [...$a, 3, 4]; var_dump($b);die; Results in this error: Parse error: syntax error, unexpected '...' (T_ELLIPSIS), expecting ']' Is using the spread syntax this way not allowed in PHP? If so, is there an equally-as-elegant way to achieve the same effect? 回答1: The

Using spread operator to update an object value

荒凉一梦 提交于 2019-11-27 14:41:18
问题 I have a function which adds a key to incoming object, but I have been told to use spread operator for that, I have been told that I can use the spread operator to create a new object with the same properties and then set isAvailable on it. return new Partner(ServerConfig, capabilities, initialState) } class Partner { constructor (ServerConfig, capabilities, initialState) { initialState.isAvailable = true So I tried something like this but coulndt succeed, can you help me ? and confused,

Spread Syntax vs Rest Parameter in ES2015 / ES6

与世无争的帅哥 提交于 2019-11-27 10:27:48
I am confused about the spread syntax and rest parameter in ES2015. Can anybody explain the difference between them with proper examples? TbWill4321 When using spread, you are expanding a single variable into more: var abc = ['a', 'b', 'c']; var def = ['d', 'e', 'f']; var alpha = [ ...abc, ...def ]; console.log(alpha)// alpha == ['a', 'b', 'c', 'd', 'e', 'f']; When using rest arguments, you are collapsing all remaining arguments of a function into one array: function sum( first, ...others ) { for ( var i = 0; i < others.length; i++ ) first += others[i]; return first; } console.log(sum(1,2,3,4)

Is It Possible To Set Default Parameter Value On A Rest Parameter

╄→尐↘猪︶ㄣ 提交于 2019-11-27 06:42:43
问题 ES6 introduces a bevy of convenient "syntactic sugar". Among them are the default parameter capabilities of JavaScript functions, as well as rest parameters. I'm finding that my console (or devTools) complains ( i.e. , throws an error) whenever attempting to set a default parameter value on a rest parameter. I've found surprisingly few references to this particular issue elsewhere and am wondering if 1.) it is possible to do so and 2.) why not (assuming it's not possible). As an example, I've