Skip to content
Advertisement

Get a specific object from an array of objects [closed]

Here is the sample input

const arr = [{name:'bruce', surname: 'wayne'}, {name:'clark', surname:'kent'}]

How to get an output that would be like

result = {bruce:'wayne', clark:'kent'}

Thank you in advance

Advertisement

Answer

You could use .reduce() like so:

const arr = [
  {name: "bruce", surname: "wayne"},
  {name: "clark", surname: "kent"}
];

const result = arr.reduce(
  (acc, curr) => ({...acc, [curr.name]: curr.surname}),
  {}
);

console.log(result);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement