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);