2

I am struggling to find a way to combine an array of objects into one object. All the solutions I have come across so far yield an array as the result, but I need an object. Here is an example:

 [
    {
        '1112225544': '1',
        '3258458756': '9',
        '3654125412': '5',
    },
    {
        '2229993827': '0',
        '9827719902': '1',
        '0000000000': '2',
        '1112225544': '3',
    },
    ...
 ]

There can be many objects inside the array. I want to flatten them and get this as the output:

{
    '3258458756': '9',
    '3654125412': '5',
    '2229993827': '0',
    '9827719902': '1',
    '0000000000': '2',
    '1112225544': '3'
}

Notice that the duplicate keys get overridden by whatever values the last array has. The solutions I have seen thus far are of this variety and don't quite work for me.

codemonkey
  • 7,325
  • 5
  • 22
  • 36

6 Answers6

5

 let data = [
    {
        '1112225544': '1',
        '3258458756': '9',
        '3654125412': '5',
    },
    {
        '2229993827': '0',
        '9827719902': '1',
        '0000000000': '2',
        '1112225544': '3',
    },
   
 ];
 
 let result = {};
 data.forEach(x=>{
 
  Object.entries(x).forEach(([k,v])=>result[k]=v)
 
 })
 
 console.log(result)

Or

let data = [
            {
                '1112225544': '1',
                '3258458756': '9',
                '3654125412': '5',
            },
            {
                '2229993827': '0',
                '9827719902': '1',
                '0000000000': '2',
                '1112225544': '3',
            },
           
         ];
         
         let result = {};
         data.forEach(x=>{
         
          result = {...result, ...x}
         
         })
         
         console.log(result)
Giorgi Moniava
  • 27,046
  • 9
  • 53
  • 90
  • 1
    Great answer and is well worth an upvote. The only reason I am going with the `reduce` answer is because, based on my benchmark, it is consistently faster than a foreach. I know speed wasn't part of the question, but since I gotta pick between two great answers, I will pick the speedier one. Thank you. – codemonkey Jul 30 '21 at 18:26
4

Here's an approach using Array.prototype.reduce:

  [
    {
      '1112225544': '1',
      '3258458756': '9',
      '3654125412': '5',
    },
    {
      '2229993827': '0',
      '9827719902': '1',
      '0000000000': '2',
      '1112225544': '3',
    },
  ].reduce(
    (acc, curr) => ({
      ...curr,
      ...acc,
    }),
    {}
  );

Whenever I have a need to "reduce" an array into either a scaler or object type, reduce is usually considered.

Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115
2

This is achievable with a single command. Use Object.assign and spread the array in.

Object.assign({}, ...[
  {
      '1112225544': '1',
      '3258458756': '9',
      '3654125412': '5',
  },
  {
      '2229993827': '0',
      '9827719902': '1',
      '0000000000': '2',
      '1112225544': '3',
  },
])
Andrew
  • 7,201
  • 5
  • 25
  • 34
1

this way ?

const arr1 = 
  [ { '1112225544': '1'
    , '3258458756': '9'
    , '3654125412': '5'
    } 
  , { '2229993827': '0'
    , '9827719902': '1'
    , '0000000000': '2'
    , '1112225544': '3'
    }
  //...
  ] 

const arr2 = arr1.reduce((r,c)=>
  {
  Object.entries(c).forEach(([k,v]) => r[k] = v )
  return r
  },{})

console.log( arr2 )
.as-console-wrapper { max-height: 100% !important; top: 0 }
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
1

Or old way:

let items = [
  {
    '1112225544': '1',
    '3258458756': '9',
    '3654125412': '5',
  },
  {
    '2229993827': '0',
    '9827719902': '1',
    '0000000000': '2',
    '1112225544': '3',
  },
];
 
let result = {};
for (let item of items)
  result = Object.assign(result, item)
console.log(result)
Robert Máslo
  • 208
  • 2
  • 5
0

let arr = [{1112225544:"1",3258458756:"9",3654125412:"5"},{2229993827:"0",9827719902:"1","0000000000":"2",1112225544:"3"}];

let result = Object.fromEntries(arr.flatMap(e => Object.entries(e)))

console.log(result)
Alan Omar
  • 4,023
  • 1
  • 9
  • 20