0

I have two arrays like below:

var oldArray = [{ id: '10', name: 'ABC', type: 'type1' },
                { id: '11', name: 'XYZ', type: 'type1' }]

var newArray=[{ id: '10', name: 'ABC', type: 'type1' },
              { id: '11', name: 'XYZ', type: 'type1' }
              { id: '12', name: 'PQR', type: 'type1' },
              { id: '13', name: 'SomeNewData', type: 'type2' }]

I want to get that PQR object from the newArray because it is of type1 and the only object which is difference in the oldArray and push that object into oldArray.

I tried it using the difference method of lodash but I couldn't get it done. Can I achieve this using lodash? Any solution using native JavaScript will also be useful.

Any help would be much appreciated. Thanks!

4
  • What about "SomeNewData"? Commented Mar 12, 2020 at 18:43
  • Are IDs unique? And by "difference", do you mean elements in newArray that are not in oldArray? Or do you want even elements in oldArray that are not in newArray? Commented Mar 12, 2020 at 18:43
  • @Mr.Polywhirl SomeNewData I won't need this because it is of type2. I only need the difference between those of type1. Commented Mar 12, 2020 at 18:45
  • @Uby I just need that PQR object with less and meaningful code using Lodash. Commented Mar 12, 2020 at 18:47

3 Answers 3

1

You could find the difference using filter and some methods and also check the type.

var oldArray = [{ id: '10', name: 'ABC', type: 'type1' }, { id: '11', name: 'XYZ', type: 'type1' }]
var newArray=[{ id: '10', name: 'ABC', type: 'type1' }, { id: '11', name: 'XYZ', type: 'type1' }, { id: '12', name: 'PQR', type: 'type1' }, { id: '13', name: 'SomeNewData', type: 'type2' }]
              
var diff = newArray.filter(({ id, type }) => (
  type == 'type1' && !oldArray.some(e => e.id == id)
))

console.log(diff)

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

3 Comments

Thank you very much for the answer. Can I get that as an object instead of inside an array?
What if you have multiple objects that pass the condition?
Btw to push each object from diff to oldArray you can use oldArray.push(...diff)
1

You can diff the two arrays by comparing the objects by equivalency. The basic isEq function below was borrowed from here: "Object Equality in JavaScript".

Once you can compare two objects, just diff them by looping over both arrays and building a difference array by checking if the current item exists in the other list.

You do this two times, once for the first list and again for the second. Except on the second time around, you check if the item is already in the diff list.

Edit: If you only want to look at items where type = "type1", filter them out before you proceed.

var oldArray = [{
  id: '10',
  name: 'ABC',
  type: 'type1'
}, {
  id: '11',
  name: 'XYZ',
  type: 'type1'
}]

var newArray = [{
  id: '10',
  name: 'ABC',
  type: 'type1'
}, {
  id: '11',
  name: 'XYZ',
  type: 'type1'
}, {
  id: '12',
  name: 'PQR',
  type: 'type1'
}, {
  id: '13',
  name: 'SomeNewData',
  type: 'type2'
}]

console.log(diff(
  oldArray.filter(item => item.type === 'type1'),
  newArray.filter(item => item.type === 'type1')
));

function diff(arrA, arrB) {
  let diff = [];
  arrA.forEach(itemA => {
    if (!arrB.some(itemB => isEq(itemA, itemB))) {
      diff.push(itemA);
    }
  })
  arrB.forEach(itemB => {
    if (!diff.some(p => isEq(itemB, p)) && !arrA.some(itemA => isEq(itemA, itemB))) {
      diff.push(itemB);
    }
  })
  return diff;
}

function isEq(a, b) {
  var aProps = Object.getOwnPropertyNames(a);
  var bProps = Object.getOwnPropertyNames(b);
  if (aProps.length != bProps.length) {
    return false;
  }
  for (var i = 0; i < aProps.length; i++) {
    var propName = aProps[i];
    if (a[propName] !== b[propName]) {
      return false;
    }
  }
  return true;
}
.as-console-wrapper { top: 0; max-height: 100% !important; }

Comments

1

How with vanilla?

var oldArray = [{
    id: '10',
    name: 'ABC',
    type: 'type1'
  },
  {
    id: '11',
    name: 'XYZ',
    type: 'type1'
  }
]

var newArray = [{
    id: '10',
    name: 'ABC',
    type: 'type1'
  },
  {
    id: '11',
    name: 'XYZ',
    type: 'type1'
  }, {
    id: '12',
    name: 'PQR',
    type: 'type1'
  },
  {
    id: '13',
    name: 'SomeNewData',
    type: 'type2'
  }
]

var existingArray = oldArray.map(arr => arr.id);

var filteredArray = newArray.filter(arr => arr.type === 'type1' && !existingArray.includes(arr.id));

var mergedArray = oldArray.concat(filteredArray);

console.log(mergedArray);

1 Comment

This is indeed useful. Thanks!

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.