0
array1 =  [
    {id: 1, height: 178}, {id: 1, height: 176},
    {id: 2, height: 168},{id: 2, height: 164}
]

array2 =  [
    {id: 1, height: ''},{id: 1, height: ''},
    {id: 2, height: ''},{id: 2, height: ''}, 
    {id: 3, height: ''}, {id: 3, height: ''}, 
    {id: 4, height: ''}, {id: 4, height: ''}
]

resultArray  = [
    {id: 1, height: 178},{id: 1, height: 176},
    {id: 2, height: 168},{id: 2, height: 164}, 
    {id: 3, height:''}, {id: 3, height: ''}, 
    {id: 4, height: ''}, {id: 4, height: ''}
]

I am looking to compare id of array1 with id of array2 and if any objects are missing in array1 then we add it to that array. Can you suggest me how to do it?

Thank you

gil
  • 1,318
  • 12
  • 19
CodingisLife
  • 109
  • 6
  • 1
    Do you want to add them incrementally or all at once? I only ask because as soon as you add one, say `id: 3`, then `array1` will have that `id` in it so you wouldn't add more – Phil Aug 13 '18 at 05:47
  • 2
    Possible duplicate of [Comparing two arrays of objects, and exclude the elements who match values into new array in JS](https://stackoverflow.com/q/32965688/9775003) – Sanoj_V Aug 13 '18 at 05:48
  • All at once because i am going to map this resultArray to render the table. i am looking to get the result array with all the missing keys in array 2 even if they were duplicated. – CodingisLife Aug 13 '18 at 05:50
  • https://stackoverflow.com/questions/19480008/javascript-merging-objects-by-id – gp. Aug 13 '18 at 05:55

3 Answers3

2

I'd recommend using a set of array1's keys for fast lookup. Note that this mutates array1:

const array1 = [{id: 1, height: 178},{id: 1, height: 176},{id: 2, height: 168},{id: 2, height: 164}]
const array2 = [{id: 1, height: ''},{id: 1, height: ''},{id: 2, height: ''},{id: 2, height: ''}, {id: 3, height: ''}, {id: 3, height: ''}, {id: 4, height: ''}, {id: 4, height: ''}]

const ids = new Set(array1.map(e => e.id));

array2.forEach(e => {
  if (!ids.has(e.id)) {
    array1.push(e);
  }
});

console.log(array1);
ggorlen
  • 44,755
  • 7
  • 76
  • 106
0

1-Save length of first array

2-Iterate through second array items

3-if the id does not exists or its index is bigger than length of array, push item to first array.

let array1 = [{ id: 1, height: 178 }, { id: 1, height: 176 }, { id: 2, height: 168 }, { id: 2, height: 164 }], array2 = [{ id: 1, height: '' }, { id: 1, height: '' }, { id: 2, height: '' }, { id: 2, height: '' }, { id: 3, height: '' }, { id: 3, height: '' }, { id: 4, height: '' }, { id: 4, height: '' }],
  arr1Length = array1.length;

array2.forEach(function(itm) {
  let index = array1.findIndex(function(it) {
    return it.id == itm.id;
  });
  if (-1 == index || arr1Length <= index) {
    array1.push(itm)
  }
});

console.log(array1)
Saeed
  • 5,413
  • 3
  • 26
  • 40
0
  1. Create a Set of id properties from array1 using something like Array.prototype.reduce() or Array.prototype.map()
  2. Filter out the entries from array2 that are not in the Set
  3. Concatenate array1 with the filtered result

const array1 =  [{id: 1, height: 178},{id: 1, height: 176},{id: 2, height: 168},{id: 2, height: 164}]
const array2 =  [{id: 1, height: ''},{id: 1, height: ''},{id: 2, height: ''},{id: 2, height: ''}, {id: 3, height: ''}, {id: 3, height: ''}, {id: 4, height: ''}, {id: 4, height: ''}]

// Step #1
const knownIds = array1.reduce((set, {id}) => set.add(id), new Set())

// Steps 2 and 3
const resultArray = array1.concat(array2.filter(({id}) => !knownIds.has(id)))

console.info(resultArray)
Phil
  • 157,677
  • 23
  • 242
  • 245