convert array to object keys javascript code example
Example 1: js array to object with keys
const subLocationTypes = (location.subLocationTypes || []).reduce((add, cur) => {
add[cur.key] = cur.value;
return add;
}, {});
Example 2: js array into object
const names = ['Alex', 'Bob', 'Johny', 'Atta'];
const obj = Object.assign({}, names);
console.log(obj);
Example 3: javascript array to object with keys
let arr = [{a: 'a', b: 1, c: 'c'}, {a: 'a', b: 2, c: 'c'}, {a: 'a', b: 3, c: 'c'}]:
let mapped = arr.reduce (function (map, obj) {
map[obj.b] = obj;
return map;
},{});
console.log (mapped);
Example 4: convert array to object javascript
const convertArrayToObject = (array, key) =>
array.reduce(
(obj, item) => ({
...obj,
[item[key]]: item
}),
{}
);
Example 5: js array to object with keys
const arr = ['a','b','c'];
const res = arr.reduce((a,b)=> (a[b]='',a),{});
console.log(res)