I have two arrays (x,y) with some number sequence, both arrays are from the same length. Now I need to combine the elements of both two arrays to a object. To create the object I have a function f How can I quickly convert it to a single array of objects
x = [1,2,3,4]
y = [5,6,7,8]
f = function(x,y) { return { foo: x, bar: y } }
... some magic ...
result = [{ foo: 1, bar: 5},
{ foo: 2, bar: 6},
{ foo: 3, bar: 7},
{ foo: 4, bar: 8}]
Of course this is possible using
const array = []
for (let i = 0; i <= x.length; i++) {
array.push(f(x[i],y[i]))
}
But I want to know if there is a more 'cleaner' way? Using .map for instance? Or would this be the way to go?
-Edit- Thanks everyone, didn't knew map could also pass the index as parameter.