How to Convert an Array of Objects to Object with Key Value Pairs

How to convert an array of objects to object with key value pairs

You could use Object.assign and a spread syntax ... for creating a single object with the given array with objects.

var array = [{ name1: "value1" }, { name2: "value2" }],    object = Object.assign({}, ...array);    console.log(object);

Convert array of objects to object of key-value pairs

I would do that with Array.prototype.reduce(), it is even more concise and certainly faster than Object.fromEntries():

const items = [{name:'hello',value:['one','two']},{name:'hi',value:['one','two','three']}], 
result = items.reduce((r,{name,value}) => (r[name]=value,r), {})
console.log(result)
.as-console-wrapper{min-height:100%;}

Convert array of object with key value pair to a single object?

Use forEach and destructuring

Update: Fixed based on Jan pointed out. Thanks @Jan

obj = [
{ key: "name", value: "jack" },
{ key: "age", value: 10 },
{ key: "country", value: "india" },
{ key: "state", value: "Delhi" },
];

const res = {};
obj.forEach(({ key, value }) => Object.assign(res, { [key]: value }));

console.log(res);

how to take a key value pair from array of objects and create a new object from that key value pair

Try this:

let arr = [
{name: "abcd", value: "xyz"},
{name: "pqr", value: "uvw"}
]
const result = {}
arr.forEach((element) => {
result[element.name] = element.value
})
console.log(result)

How to convert an array of objects into key value pairs

You were close, just missing that when you set up the object in the resultant array, you need to set it as an array - in this script that would be [k]: [v] and not [k]: v

var contacts = [
{ account: "Acme", firstName: "John", lastName: "Snow" },
{ account: "Metal Industries", firstName: "Ted", lastName: "Smith" },
{ account: "Acme", firstName: "Sara", lastName: "Butler" },
{ account: "HiTech Corp", firstName: "Sam", lastName: "Johnson" },
{ account: "HiTech Corp", firstName: "Arnold", lastName: "Williams" },
{ account: "Metal Industries", firstName: "Jessica", lastName: "Westcoat" },
{ account: "Acme", firstName: "Kyle", lastName: "Johnson" },
{ account: "HiTech Corp", firstName: "Jason", lastName: "Fernandez" }
];

const convertArrayToObject = (array, key) => {
const initialValue = {}
return array.reduce((obj, item) => {
let k = item[key], v=item.firstName + ' ' + item.lastName;
if (obj[k]) obj[k].push(v);
else obj = { ...obj, [k]: [v] }
return obj
}, initialValue)
}

console.log(convertArrayToObject(contacts, 'account'))

how to loop and convert an array of objects to an object with key value pair

This can be done using Array.prototype.reduce func.

const input = [
{name: "John", country: "USA", revision: 1},
{name: "Mark", country: "England", revision: 0},
{name: "Bruce", country: "France", revision: 1}
];

const output = input.reduce((acc, {name, ...item}) => {
acc[name] = item;
return acc;
}, {});
console.log(output);


Related Topics



Leave a reply



Submit