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

4
  • 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 Commented Aug 13, 2018 at 5:47
  • 2
    Possible duplicate of Comparing two arrays of objects, and exclude the elements who match values into new array in JS Commented Aug 13, 2018 at 5: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. Commented Aug 13, 2018 at 5:50
  • stackoverflow.com/questions/19480008/… Commented Aug 13, 2018 at 5:55

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Nice! I had a feeling there'd be a nicer way to build the Set than using reduce()
Great!! This is what I am looking for. Works perfect for me. Thank you very much
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)

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.