-1

I have object like this

[
  { A: '1' },
  { B: '2' },
  { C: '3' },
]

I want convert to like this

{
  A: '1',
  B: '2',
  C: '3',
}

what is the best way to do it

ray
  • 3
  • 3

3 Answers3

1
  • Object.assign with spread syntax
let arr = [
  { A: '1' },
  { B: '2' },
  { C: '3' },
]

console.log(Object.assign(...arr));
Dreamy Player
  • 556
  • 5
  • 18
  • 1
    It's notable, that this mutates the first object in the original array. If that's not desirable, pass an empty object as `this` argument to `.assign` (`Object.assign({}, ...arr)`. – Teemu Mar 17 '22 at 06:38
0

let arr = [
  { A: '1' },
  { B: '2' },
  { C: '3' },
]

let output = {}

arr.map((obj) => {
    output = {...output, ...obj}
})

console.log(output)
dinesh oz
  • 342
  • 1
  • 9
0

Object.assign and spread operator will do the trick :

let arrObject = [
   { A: "1" }, 
   { B: "2" },
   { C: "3" }
]; 
  1. Object.assign(target, ...sources)

let obj = Object.assign({}, ...arrObject);

console.log(obj) => obj = { A: '1', B: '2', C: '3' }