From
[
{
"id": "0001",
"name": "Jack",
"completeName": "Jack Smith"
},
{
"id": "0002",
"name": "Mary",
"completeName": "Mary Jackson"
},
{
"id": "0003",
"name": "John",
"completeName": "John Doe"
}
]
to this:
{
"Jack" :{
"id": "0001",
"name": "Jack",
"completeName": "Jack Smith"
},
"Mary" : {
"id": "0002",
"name": "Mary",
"completeName": "Mary Jackson"
},
"John": {
"id": "0003",
"name": "John",
"completeName": "John Doe"
}
}
Barmar
789k57 gold badges555 silver badges669 bronze badges
5 Answers 5
You can use .map() and Object.fromEntries().
var data = [{
"id": "0001",
"name": "Jack",
"completeName": "Jack Smith"
},
{
"id": "0002",
"name": "Mary",
"completeName": "Mary Jackson"
},
{
"id": "0003",
"name": "John",
"completeName": "John Doe"
}
];
var result = Object.fromEntries(data.map(el => [el.name, el]));
console.log(result);
answered Jul 25, 2021 at 13:30
Barmar
789k57 gold badges555 silver badges669 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Steve
How about if using this code? const arrayToObject = () => { let test: any = {} // insert loop here console.log('test: %o ', test) } arrayToObject()
Barmar
There's certainly nothing wrong with putting the code inside a function if you need to use it multiple times. But the function should take the object as a parameter. You might also want to make the property to use as the keys a parameter as well.
1. using forEach :
var data = [{
"id": "0001",
"name": "Jack",
"completeName": "Jack Smith"
},
{
"id": "0002",
"name": "Mary",
"completeName": "Mary Jackson"
},
{
"id": "0003",
"name": "John",
"completeName": "John Doe"
}
];
var result = {};
data.forEach(function(val) {
result[val["name"]] = val;
});
console.log(result);
2. Using object.assign and map:
var data = [{
"id": "0001",
"name": "Jack",
"completeName": "Jack Smith"
},
{
"id": "0002",
"name": "Mary",
"completeName": "Mary Jackson"
},
{
"id": "0003",
"name": "John",
"completeName": "John Doe"
}
];
let dictionary = Object.assign({}, ...data.map((x) => ({
[x.name]: x
})));
console.log(dictionary);
answered Jul 25, 2021 at 13:26
Abhi
1,0251 gold badge9 silver badges14 bronze badges
2 Comments
Dmitry Kostyuk
forEach() isn't meant to change data, only loop through it. map(), filter() and reduce() are. These are functional programming methods, so it's best practice to keep them pure, meaning they shouldn't be used to change data outside their scope, however changing data with forEach() forces you to do just that by using a global variable.Abhi
OP asked for forEach, map or other ways to get the result. It will certainly not break the code knowing different possible ways and it highly depends on the usecase.
You can use an array reduce method. Array.prototype.reduce()
const test = [
{
"id": "0001",
"name": "Jack",
"completeName": "Jack Smith"
},
{
"id": "0002",
"name": "Mary",
"completeName": "Mary Jackson"
},
{
"id": "0003",
"name": "John",
"completeName": "John Doe"
}
];
const results = test.reduce((obj, props) => {
obj[props.name] = props;
return obj;
} , {});
console.log(results);
answered Jul 25, 2021 at 13:42
Mamunur Rashid
1,17719 silver badges38 bronze badges
3 Comments
Steve
Can i use forEach loop only?
Mamunur Rashid
Yes, you can use it, however, there is a lot of ways to convert an array to object
Dmitry Kostyuk
forEach() isn't meant to change data, only loop through it. map(), filter() and reduce() are. These are functional programming methods, so it's best practice to keep them pure, meaning they shouldn't be used to change data outside their scope, however changing data with forEach() forces you to do just that by using a global variable.You can use Array.prototype.reduce() like so:
const data = [{
"id": "0001",
"name": "Jack",
"completeName": "Jack Smith"
}, {
"id": "0002",
"name": "Mary",
"completeName": "Mary Jackson"
}, {
"id": "0003",
"name": "John",
"completeName": "John Doe"
}];
const usersByName = data.reduce((acc, cur) => {
acc[cur.name] = cur;
return acc;
}, {});
console.log(usersByName);
const data = [{
"id": "0001",
"name": "Jack",
"completeName": "Jack Smith"
}, {
"id": "0002",
"name": "Mary",
"completeName": "Mary Jackson"
}, {
"id": "0003",
"name": "John",
"completeName": "John Doe"
}];
const usersByName = Object.fromEntries(data.map(entry => [entry.name, entry]))
console.log(usersByName);
Or a Map:
const data = [{
"id": "0001",
"name": "Jack",
"completeName": "Jack Smith"
}, {
"id": "0002",
"name": "Mary",
"completeName": "Mary Jackson"
}, {
"id": "0003",
"name": "John",
"completeName": "John Doe"
}];
const usersByName = new Map(data.map(entry => [entry.name, entry]));
console.log(Object.fromEntries(usersByName.entries()));
While the Array.prototype.reduce() option might be the most common one, as it has existed for many years, I find the more recent Object.entries() alternative quite intuitive.
answered Aug 22, 2023 at 17:03
Danziger
21.4k6 gold badges59 silver badges91 bronze badges
Comments
You need to take your data, let's say it's stored in the data variable, and reduce() it with this one-liner:
const output = data.reduce((acc, row) => ({ ...acc, [row.name]: row }), {} );
const data = [
{
id: '0001',
name: 'Jack',
completeName: 'Jack Smith',
},
{
id: '0002',
name: 'Mary',
completeName: 'Mary Jackson',
},
{
id: '0003',
name: 'John',
completeName: 'John Doe',
},
];
const output = data.reduce((acc, row) => ({ ...acc, [row.name]: row }), {});
console.log(output);
answered Jul 25, 2021 at 13:56
Dmitry Kostyuk
1,4691 gold badge9 silver badges24 bronze badges
Comments
lang-js
reduce- create a new target object, walk all entries inreduceand add them on the fly to the target object withel.nameas keys. Done.