convert array to object in js code example
Example 1: js array into object
const names = ['Alex', 'Bob', 'Johny', 'Atta'];
const obj = Object.assign({}, names);
console.log(obj);
Example 2: convert array to object javascript
const convertArrayToObject = (array, key) =>
array.reduce(
(obj, item) => ({
...obj,
[item[key]]: item
}),
{}
);
Example 3: javascript convert array to object
function arrayToObject(arr) {
var obj = {};
for (var i = 0; i < arr.length; ++i){
obj[i] = arr[i];
}
return obj;
}
var colors=["red","blue","green"];
var colorsObj=arrayToObject(colors);