bla
bla

Reputation: 1055

How i can merge this array of object?

I have this array of objects:

let array1 = [{
      id: 1,
      code: 'wawa'
    },
    {
      id: 2,
      code: 'qwqw'
    }]

    let array2 = [{
      id: 1,
      code: 'ewew'
    },
    {
      id: 3,
      code: 'eee'
    }]

    let array3 = [{
      id: 3,
      code: 'ww'
    },
    {
      id: 1,
      code: 'ww'
    },
    {
      id: 4,
      code: 'q'
    }
  ]

  let arrayGeral = new Array()
  arrayGeral.push(array1)
  arrayGeral.push(array2)
  arrayGeral.push(array3)

How i can merge the objects in the arrayGeral to a one array object?

I can do what i want using lodash, but i can't think in a way to iterate and make this automatic:

let merged = _.merge(_.keyBy(array1, 'id'), _.keyBy(array2, 'id'))
merged = _.merge(_.keyBy(merged, 'id'), _.keyBy(array3, 'id'))

Upvotes: 0

Views: 56

Answers (3)

David
David

Reputation: 16277

If this is your output:

[                                                                   
   { id: 1, code: [ 'wawa', 'ewew', 'ww' ] },                          
   { id: 2, code: [ 'qwqw' ] },                                        
   { id: 3, code: [ 'eee', 'ww' ] },                                   
   { id: 4, code: [ 'q' ] }                                            
]                                                                     

Then refer to the below code:

let array1 = [{ id: 1, code: 'wawa'}, { id: 2, code: 'qwqw' }]
let array2 = [{ id: 1, code: 'ewew'}, { id: 3, code: 'eee'  }]
let array3 = [{ id: 3, code: 'ww'  }, { id: 1, code: 'ww'   }, 
              { id: 4, code: 'q'   }]

var concated = array1.concat(array2).concat(array3);  //Flaten the concatenated array
let output = concated.reduce((acc, cur) => {     
    let entry = acc.find(item => item.id === cur.id);
    if (entry && Array.isArray(entry.code)) { //Check if it has sth in the array
            entry.code.push(cur.code);
        }
    else  {
      acc.push({id: cur.id, code: [cur.code]}); //For the 1st time, add it to the array
    }
    //console.log(acc);
    return acc;
}, []);
console.log(output);

Upvotes: 1

Mihai Matei
Mihai Matei

Reputation: 24276

I suppose that the last values should be kept. If that's the case then you must inverse the way you push:

let arrayGeral = new Array();
arrayGeral.push(...array3);
arrayGeral.push(...array2);
arrayGeral.push(...array1);

Now all you have to do is to use the _.uniqBy lodash function:

_.uniqBy(arrayGeral, 'id');

let array1 = [
  {
    id: 1,
    code: 'wawa'
  },
  {
    id: 2,
    code: 'qwqw'
  }
];

let array2 = [
  {
    id: 1,
    code: 'ewew'
  },
  {
    id: 3,
    code: 'eee'
  }
];

let array3 = [
  {
    id: 3,
    code: 'ww'
  },
  {
    id: 1,
    code: 'ww'
  },
  {
    id: 4,
    code: 'q'
  }
];

let arrayGeral = new Array();
arrayGeral.push(...array3);
arrayGeral.push(...array2);
arrayGeral.push(...array1);

console.log(_.uniqBy(arrayGeral, 'id'));
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

Upvotes: 1

PEPEGA
PEPEGA

Reputation: 2271

I am assuming you want to merge them to 1 array without grouping by id. If that is the case you can use concat to merge them to 1 array like so: mergedArray = array1.concat(array2, array3)

let array1 = [{
      id: 1,
      code: 'wawa'
    },
    {
      id: 2,
      code: 'qwqw'
    }]

    let array2 = [{
      id: 1,
      code: 'ewew'
    },
    {
      id: 3,
      code: 'eee'
    }]

    let array3 = [{
      id: 3,
      code: 'ww'
    },
    {
      id: 1,
      code: 'ww'
    },
    {
      id: 4,
      code: 'q'
    }
  ]  

var concatArr = array1.concat(array2, array3);
console.log(concatArr);

Upvotes: 1

Related Questions