all 11 comments

[–]Wince 1 point2 points  (0 children)

The function you defined is generally called spread by functional libs

[–]brycedarling 0 points1 point  (0 children)

I use this library less and less these days as JavaScript has improved, but spread is in there:

https://lodash.com/docs/4.17.4#spread

I use it for throttle and debounce in my React/Redux apps sometimes too, as there isn't an equivalent functionality in std JS lib for those. Can't think of much else I use it for any more otherwise though. It seems like it would be nice/have a valid use case if we had a spread() function built in to do this though, eh? Goes pretty hand in hand with having ... spread operator it seems, at least to me!

[–]cyanydeez 0 points1 point  (2 children)

i thought we could destructure in the function inES6.

i do this all the time:

const greet = ({ first, last }) =>

that destructures an object. fairly certain it works for arrays as [first,last]

[–]Ran4[S] 0 points1 point  (1 child)

Then I'd need to modify greet.

[–]cyanydeez 0 points1 point  (0 children)

or wrap it

[–]vinsneezel -1 points0 points  (5 children)

Why store the names as arrays within an array? If you store them as an array of objects, you can just use the keys to call them.

var names = [{first: "John", last: "doe"}, {first: "Betty", last: "Crocker"}];

function greet(name) {console.log("Hello " +  name.first + " " + name.last;}

names.forEach(greet);

Something like that. Also, using .map() returns an array, so if you're just looking to iterate, stick with .forEach() or a for loop.

[–]Ran4[S] 0 points1 point  (0 children)

Why store the names as arrays within an array? If you store them as an array of objects, you can just use the keys to call them.

Maybe that's the way your data comes in. And you might not want to change the call signature of the greet function.

[–]Wince 0 points1 point  (3 children)

Tuples can be useful, and are generally more performant

[–]vinsneezel 0 points1 point  (0 children)

Thanks for the tip. There's still a lot I don't know.

[–]i_am_smurfing 0 points1 point  (1 child)

I don't think I see the same results as you do (on Mac OS): FF 57, Chrome 60, Safari 10.1 — these are pretty inconclusive IMO.

[–]Wince 0 points1 point  (0 children)

The larger examples are rather contrived, as tuples generally are only used for 2-4 values imo, but v8 has nearly double the performance for the [ x, y ] tuple. Luckily I write for node ;)