0

I have come accross this syntax in a tutorial. Some say its not ES6 syntax. it was in a reduce function. I need a clear explanation. What is going on in these parentethes ?

{...curr, ...acc}

full code..

const endShape = _(raw)
.filter(({key}) =>!/garbage/.test(key))
.map(({key,value})=>({[key]:value}))
.reduce((acc,curr)=>({...curr, ...acc}));
console.log(endShape);
henrybbosa
  • 1,139
  • 13
  • 28
  • I find using something like babel's online REPL useful for understanding some of the most interesting use of new JS sytntax - https://babeljs.io/repl/#?babili=false&evaluate=true&lineWrap=false&presets=env%2Cstage-0&targets=&browsers=&builtIns=false&debug=false&experimental=true&loose=false&spec=false&code_lz=MYewdgzgLgBApmAJgZQBYEMAOcYF4YD6AFAE7oDuAlAFAB0AZgJYA2UcJRRA3gNZwCeAX0p4AfAEIA9AHN0JAEbppcSbTbQiffpRq0Atlk68BAGgBu6ZgFc4w3KO4BtLQF0AXBeu2ddEnERWwHCc6MDAJsBWJCSU9ty0CZHRJjAJtKHAwpQA3NSgkCDMcLTMINJECCgY2DlAA – Jaromanda X Aug 14 '17 at 09:56

1 Answers1

-1

Spread Operator Shorthand The spread operator, introduced in ES6, has several use cases that make JavaScript code more efficient and fun to use. It can be used to replace certain array functions. The spread operator is simply a series of three dots.

Longhand

const odd = [1, 3, 5];
const nums = [2 ,4 , 6].concat(odd);
// cloning arrays
const arr = [1, 2, 3, 4];
const arr2 = arr.slice();

Shorthand

// joining arrays
const odd = [1, 3, 5 ];
const nums = [2 ,4 , 6, ...odd];
console.log(nums); // [ 2, 4, 6, 1, 3, 5 ]

// cloning arrays
const arr = [1, 2, 3, 4];
const arr2 = [...arr];
Osama
  • 2,912
  • 1
  • 12
  • 15