spread-syntax

What is the cleanest way to remove an element from an immutable array in JS? [duplicate]

痴心易碎 提交于 2019-12-10 02:52:02
问题 This question already has answers here : Is this the correct way to delete an item using redux? (4 answers) Closed 2 years ago . I need to remove an element from an array that is a state of a React Component. Which means that it is an immutable object. Adding a element is easy using spread syntax. return { ...state, locations: [...state.locations, {}] }; Removing is a little more tricky. I need to use an intermediate object. var l = [...state.locations] l.splice(index, 1) return { ...state,

Javascript Proxy and spread syntax, combined with console.log

允我心安 提交于 2019-12-09 16:40:56
问题 So, I was playing around with Proxy objects and while trying to see how they mix with spread syntax and de-structuring, I stubled upon this weird behavior: const obj = { origAttr: 'hi' } const handler = { get(target, prop) { console.log(prop); return 1; }, has(target, prop) { return true; }, ownKeys(target) { return [...Reflect.ownKeys(target), 'a', 'b']; }, getOwnPropertyDescriptor(target, key) { return { enumerable: true, configurable: true }; } } const test = new Proxy(obj, handler); const

How does the spread syntax affect array splice

巧了我就是萌 提交于 2019-12-09 07:28:47
问题 I found the following code and I don't know what is the difference between A and B: var fruits = ["Banana", "Orange", "Apple", "Mango"]; A fruits.splice(2,0,["Lemon", "Kiwi"]); B fruits.splice(...[2,0].concat(["Lemon", "Kiwi"])); var fruits = ["Banana", "Orange", "Apple", "Mango"]; var A = fruits.splice(2, 0, ["Lemon", "Kiwi"]); var B = fruits.splice(...[2, 0].concat(["Lemon", "Kiwi"])); console.log(A) console.log(B) 回答1: First of all, Statement A & Statement B will generate different results

Spread Operator not working for Redux/ES6 based sample

只谈情不闲聊 提交于 2019-12-05 08:18:59
I am trying to understand Redux online tutorials that are posted by Dan Abramov. At present I am on the following sample: Reducer composition with Arrays Here is my practice code following the above sample: // Individual TODO Reducer const todoReducer = (state, action) => { switch(action.type) { case 'ADD_TODO': return { id: action.id, text: action.text, completed: false }; case 'TOGGLE_TODO': if (state.id != action.id) return state; // This not working /* return { ...state, completed: !state.completed }; */ //This works var newState = {id: state.id, text: state.text, completed: !state

What is the cleanest way to remove an element from an immutable array in JS? [duplicate]

眉间皱痕 提交于 2019-12-05 02:44:45
This question already has answers here : Is this the correct way to delete an item using redux? (4 answers) Closed 2 years ago . I need to remove an element from an array that is a state of a React Component. Which means that it is an immutable object. Adding a element is easy using spread syntax. return { ...state, locations: [...state.locations, {}] }; Removing is a little more tricky. I need to use an intermediate object. var l = [...state.locations] l.splice(index, 1) return { ...state, locations: l } It make the code more dirt and difficult to understand. Is there an easier or less tricky

using the … spread syntax in javascript es6 named exports

橙三吉。 提交于 2019-12-04 16:03:18
问题 I am attempting to import everything from a library as a hash, modify it, and re-export the modified hash, without knowing all of the named exports in a library. For example: import * as reactBootstrap from 'react-bootstrap'; wrappedReactBootstrap = doFunnyThingsTo(reactBootstrap); export { ...wrappedReactBootstrap }; // or export wrappedReactBootstrap; My understanding of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export is that the following is not allowed

Javascript ES6 spread operator on undefined [duplicate]

删除回忆录丶 提交于 2019-12-04 15:26:39
问题 This question already has an answer here : Spreading undefined in array vs object (1 answer) Closed 2 years ago . While developing my react App, I needed to send a conditional prop to a component so I found somewhere a pattern to do so, although it seems really weird to me and I couldn't understand how and why it worked. If I type: console.log(...undefined) // Error console.log([...undefined]) // Error console.log({...undefined}) // Work When the spread operator is activated on undefined an

Javascript Proxy and spread syntax, combined with console.log

て烟熏妆下的殇ゞ 提交于 2019-12-04 05:04:35
So, I was playing around with Proxy objects and while trying to see how they mix with spread syntax and de-structuring, I stubled upon this weird behavior: const obj = { origAttr: 'hi' } const handler = { get(target, prop) { console.log(prop); return 1; }, has(target, prop) { return true; }, ownKeys(target) { return [...Reflect.ownKeys(target), 'a', 'b']; }, getOwnPropertyDescriptor(target, key) { return { enumerable: true, configurable: true }; } } const test = new Proxy(obj, handler); const testSpread = { ...test}; console.log('Iterate test'); // Works OK, output as expected for (const i in

spread syntax vs slice method

喜欢而已 提交于 2019-12-03 05:26:43
I was trying to understand what is the difference between spread syntax vs slice method in the following approach. suppose I want to make an actual copy of an array, I can probably easily do it using spread syntax var fruits = ["Banana", "Chips" , "Orange", "Lemon", "Apple", "Mango"] var newCitrus = [...fruits] If I console.log this ["Banana", "Chips", "Orange", "Lemon", "Apple", "Mango"] but I can also create a copy of an array using the slice method. Considering the same array above, if I do something like this... var citrus = fruits.slice(0); and then console log it, it will give me exactly

Using spread syntax and new Set() with typescript

早过忘川 提交于 2019-12-02 19:48:22
I am using following code to get unique numbers: let uniques = [ ...new Set([1, 2, 3, 1, 1]) ]; // [1, 2, 3] However, typescript report following error: Type 'Set' is not an array type. I am not typescript ninja, could someone tell me what is wrong here? This is a missing feature. TypeScript only supports iterables on Arrays at the moment. Update : With Typescript 2.3, you can now add "downlevelIteration": true to your tsconfig, and this will work while targeting ES5. The downside of downlevelIteration is that TS will have to inject quite a bit of boilerplate when transpiling. The single line