I have a collection of objects, each object containing different names.
[ "John", "Alice", "Bob", "Eve" ]
There is also another array of objects:
[ { name:"Alice", value: 345 }, { name: "John", value: 678 }, { name: "Eve", value: 987 } ]
To create a new array, I want each element to be a sub-array with the name and value pairs. The order of elements should match the first array of names.
For example:
[ ["John", 678], ["Alice", 345], ["Bob", undefined], ["Eve", 987] ]
However, when I tried to implement this, my output was incorrect as the values were not in the correct order.
var order = ["John", "Alice", "Bob", "Eve"];
var values = [{ name: "Alice", value: 345 }, { name: "John", value: 678 }, { name: "Eve", value: 987 }];
var newArray = order.map(function(name, i) {
return [name, (values[i] && values[i].value)];
})
console.log(newArray)