0

this time I have the following problem, I'm getting from my database an array of objects, which is updated every few minutes, the problem is that this array can come with new objects or deleted objects compared to its previous array.. example;

first data entry

[
  {name: 'Paul', color: 'blue', age: '7'},
  {name: 'Lisa', color: 'rose', age: '4'}
]

second data entry ( an object is added )

[
  {name: 'Paul', color: 'blue', age: '7'},
  {name: 'Lisa', color: 'rose', age: '4'},
  {name: 'Adam', color: 'green', age: '11'}
]

third data entry ( an object is deleted )

[
  {name: 'Paul', color: 'blue', age: '7'},
  {name: 'Adam', color: 'green', age: '11'}
]

I need to detect and verify which element was added or deleted compared to the first entry, try to do a forEach to do it through the keys but it is impossible to use it since the order of the objects varies.

How could I get these differences?

2
  • Sort both arrays, then use a foreach loop? Commented Apr 13, 2019 at 18:13
  • @melpomene how to achieve it if probably the arrays do not have the same length? Commented Apr 13, 2019 at 18:32

2 Answers 2

1

after battling and trying to find an optimal solution I have managed to do the following ....

It works well for what I need, I'm pretty basic in JS I hope I'm not committing a bad practice, I share it with anyone who may need it.

See you later.

let before = [
  {name: 'Paul', color: 'blue', age: '7'},
  {name: 'Lisa', color: 'rose', age: '4'}
]

// second data entry ( an object is added )

 let after = [
  {name: 'Paul', color: 'blue', age: '7'},
  {name: 'Lisa', color: 'rose', age: '4'},
  {name: 'Adam', color: 'green', age: '11'}
]

// third data entry ( an object is deleted )
let afterD = [
  {name: 'Paul', color: 'blue', age: '7'},
  {name: 'Adam', color: 'green', age: '11'}
]

// new element
let newElement = after.filter(element => {
  return !before.some(e => element.name === e.name);
});

// delete element
let deleteElement = before.filter(element => {
  return !afterD.some(e => element.name === e.name);
});

console.log('new element: ')
console.log(newElement)
console.log('delete element: ')
console.log(deleteElement)

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

Comments

0

Firstly you need to check of their length beacuse if new array length is bigger than old you need to loop in new array else you need to loop in old array.That is most important point and you can find difference from 2 arrays.

 var oldVal = [
      {name: 'Paul', color: 'blue', age: '7'},
      {name: 'Lisa', color: 'rose', age: '4'}
    ];
    var newVal = [
      {name: 'Paul', color: 'blue', age: '7'},
      {name: 'Lisa', color: 'rose', age: '4'},
      {name: 'Adam', color: 'green', age: '11'},
      {name : 'xxx'}
    ];

    var t1=[];
    var t2=[];
    if(newVal.length >= oldVal.length){
      t1=newVal;
      t2=oldVal;
    }  
    else{
      t1=oldVal;
      t2=newVal;
    }


    var dif=[];
    t1.forEach(i => {
      var res = t2.findIndex(x => JSON.stringify(x) == JSON.stringify(i));
      console.log(res);
      if(res == -1 ){
        dif.push(i);
      }
    })
    console.log(dif);

and result will be;

0: {name: "Adam", color: "green", age: "11"}
1: {name: "xxx"}

3 Comments

this probable solution differentiates the array and gets if a new object was added unlike the initial array, now how do you know if an object was removed from the initial array?
Could you edit it to achieve the above mentioned? Thank you.!
I couldnt compare object has different keys so I find solution with findIndex(x => JSON.stringify(x) == JSON.stringify(i));

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.