0

My question is pretty simple. How do you spread the result of .map object?

Because I currently have this syntax

let currentObject = ViewsObject.filter(e => e.CurrentView).map(t => t);

And everytime I use the variable "currentObject" I need to use spread operator on it like hits

GetAllMainItems(...currentObject).then(function(GetAllMainItemsResults) { 
 console.log(GetAllMainItemsResults)
})

And it works perfectly using spread operator evertime I use "currentObject" variable, the problem is I am planning to use it multiple times and I dont want to use spread operator evertime I use it the said variable, So to make it effecient is there a way to use spread operator inside the .map?

Hawk
  • 514
  • 1
  • 7
  • 22
  • 1
    What's the problem with using spread there? IMO it's perfectly fine, it's not inefficient (you're not creating a new array or anything like that, you're just spreading into an argument list) – CertainPerformance Nov 14 '18 at 05:40
  • 1
    Spreading into arguments is a shorthand for `GetAllMainItems(currentObject[0], currentObject[1], ...)`. There's no shortcut to prepare that in advance and then just do `GetAllMainItems(SpreadCurrentObject)`, that would not result in multiple arguments. – deceze Nov 14 '18 at 05:44
  • Oh I see, I thought since I had to use it multiple times its becoming redundant and wasting resource. But since its not much of an impact then spread operator is ok – Hawk Nov 14 '18 at 05:44
  • 3
    Not sure if it’s simplified for the example but `.map(t => t)` generally doesn’t do anything FWIW. – Ry- Nov 14 '18 at 05:50
  • I wonder if argument spreading might take a tiny bit more processing than `apply`, due to having to be able to handle situations like `fn(...foo, 'bar', ...baz)`, but even if so, the extra load would be quite minimal, there are surely more important things to micromanage – CertainPerformance Nov 14 '18 at 05:53
  • [`...` is not an operator!](https://stackoverflow.com/questions/37151966/what-is-spreadelement-in-ecmascript-documentation-is-it-the-same-as-spread-oper/37152508#37152508) – Felix Kling Nov 14 '18 at 06:21

0 Answers0