I have 2 arrays -
1- Numbers = [3,4,5]
2- selection= [ [1,2,3,4],[6,5,4,3],[2,9,4]]
Now in output I want 3 should be key i.e index of [1,2,3,4]
and so on
Output should be below -
selection= = { '3':[1,2,3,4] ,'4':[6,5,4,3] ,'5':[2,9,4]}
I have 2 arrays -
1- Numbers = [3,4,5]
2- selection= [ [1,2,3,4],[6,5,4,3],[2,9,4]]
Now in output I want 3 should be key i.e index of [1,2,3,4]
and so on
Output should be below -
selection= = { '3':[1,2,3,4] ,'4':[6,5,4,3] ,'5':[2,9,4]}
just use _.zipObject https://lodash.com/docs/4.17.2#zipObject
_.zipObject(Numbers, selection)
in plain Javascript, you could iterate numbers
and build a new object with a number as key and take as value the item of selection
with the same index.
var numbers = [3, 4, 5],
selection = [[1, 2, 3, 4], [6, 5, 4, 3], [2, 9, 4]],
result = {};
numbers.forEach(function (k, i) {
result[k] = selection[i];
});
console.log(result);
ES6
var numbers = [3, 4, 5],
selection = [[1, 2, 3, 4], [6, 5, 4, 3], [2, 9, 4]],
result = numbers.reduce((r, k, i) => Object.assign(r, { [k]: selection[i] }), {});
console.log(result);