spread-syntax

spread syntax vs slice method

巧了我就是萌 提交于 2019-12-20 19:38:08
问题 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

Iterate over array of objects and change one property in each object

核能气质少年 提交于 2019-12-18 20:49:33
问题 I find myself presented with this pattern quite a bit. I have an array of objects that I get back from my api, and I need to manipulate just one of the properties in all of the objects. Is there a way using ES6/Babel or Typescript to get that pattern to be a little more declarative? Looking for some neat destructuring trick or something along those lines. const data = [{ foo: 1, bar: 2}, { foo: 2, bar: 3}, { foo: 3, bar: 4}]; const increment = a => a + 1; // Here is my typical pattern const

Difference between using a spread syntax (…) and push.apply, when dealing with arrays

三世轮回 提交于 2019-12-18 12:49:14
问题 I have two arrays, const pets = ["dog", "cat", "hamster"] const wishlist = ["bird", "snake"] I want to append wishlist to pets , which can be done using two methods, Method 1: pets.push.apply(pets,wishlist) Which results in: [ 'dog', 'cat', 'hamster', 'bird', 'snake' ] Method 2: pets.push(...wishlist) Which also results in: [ 'dog', 'cat', 'hamster', 'bird', 'snake' ] Is there is a difference between these two methods in terms of performance when I deal with larger data? 回答1: Both Function

Deep copy in ES6 using the spread syntax

我的梦境 提交于 2019-12-17 08:53:23
问题 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

Deep copy in ES6 using the spread syntax

不想你离开。 提交于 2019-12-17 08:52:11
问题 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

Why can't I remove the intermediate variable in my code?

牧云@^-^@ 提交于 2019-12-12 12:58:21
问题 I'm currently working with the spread syntax and ran into an unexpected issue. The below snippet works (as expected), and doesn't throw any errors: const arr = [1, 2, 3, 4] // create array of numbers const copy = [...arr] // make a shallow copy of the array copy.forEach(n => { // loop through array console.log(n + 1); }); However, if I remove the intermediate variable copy , my code seems to throw an error: const arr = [1, 2, 3, 4] // create array of numbers [...arr].forEach(n => { // loop

Using spread syntax and new Set() with typescript

Deadly 提交于 2019-12-12 07:09:44
问题 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? 回答1: This is a missing feature. TypeScript only supports iterables on Arrays at the moment. 回答2: Update : With Typescript 2.3, you can now add "downlevelIteration": true to your tsconfig, and this will work while targeting ES5. The downside

How does the spread syntax in ES6 JS work as a function argument?

那年仲夏 提交于 2019-12-11 13:57:06
问题 OK, so I'm trying to understand how the new spread works as a function parameter. Imagine a function with an unknown number of parameters and adds them all together. let addNums = ...a => a.reduce ((a,b) => a+b); This function obviously works. So, what's my problem? Well here's some observations, followed by my problem: the spread syntax as a function parameter / argument seems designed to 'spread' an array of values as separate arguments: Based on the research I've done, the spread syntax

Can I use object spread syntax in angular + ngrx?

倾然丶 夕夏残阳落幕 提交于 2019-12-11 07:17:24
问题 I was reading from here about the object spread syntax and I'm trying to use it in my project, my setup is the following: angular 2 angular/cli 1.0.0-rc.0 ngrx/core 1.2.0 ngrx/store 2.2.1 rxjs 5.1.0 typescript 2.0.10 In my reducer.ts I have export interface State { [id: number]: string } export function reducer(state= {}, action: Action): State { case 'TEST': return { ...state, 2: 'foo' } } But I got the following compiling error, I'm trying to figure out what's wrong: Property assignment

Filtering empty strings from a string containing emojis using spread syntax

有些话、适合烂在心里 提交于 2019-12-11 06:34:46
问题 I'm trying to stay hip, so I've been playing with the spread operator and emojis. I noticed that when I want to filter empty strings ( '' ) out the resulting "spread-ed" array, the empty strings are not being removed. Why is that? console.log([...'😀︎']); // ['😀︎', ''] console.log([...'😀︎'].filter(String)); // ['😀︎', ''] console.log(['😀︎', ''].filter(String)); // ['😀︎'] 回答1: There is in your string an invisible character, which is a variation selector. You can see this if you print the