Here is the sample input
JavaScript
x
2
1
const arr = [{name:'bruce', surname: 'wayne'}, {name:'clark', surname:'kent'}]
2
How to get an output that would be like
JavaScript
1
2
1
result = {bruce:'wayne', clark:'kent'}
2
Thank you in advance
Advertisement
Answer
You could use .reduce()
like so:
JavaScript
1
11
11
1
const arr = [
2
{name: "bruce", surname: "wayne"},
3
{name: "clark", surname: "kent"}
4
];
5
6
const result = arr.reduce(
7
(acc, curr) => ({acc, [curr.name]: curr.surname}),
8
{}
9
);
10
11
console.log(result);