-2

I need some help checking for array of objects changed. For example i have a array of objects, and i need to detect after is changed if his length or properties values got changed Case it got changed anything from the object it gives me a boolean response.

Is there a lodash function usefull to this?

Array Object example:

[{name: "James", age: 17}, {name: "Maria", age: 17},...]
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
Marco Santos
  • 915
  • 2
  • 11
  • 24

4 Answers4

0

Lodash's isEqual function helps you in this case:

_.isEqual(firstArray, secondArray)

Note that order is important for arrays:

_.isEqual([1, 2], [2, 1]) // false
TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
0

If you want to check two object based on properties the you can do something like this.

var obj1 = {name: "James", age: 17}
var obj2 = {name: "Maria", age: 17}

JSON.stringify(obj1) === JSON.stringify(obj2)// Returns false

Modify the obj2 name property to "James"

 obj2 = {name: "Maria", age: 17}
 JSON.stringify(obj1) === JSON.stringify(obj2)// Returns true

Let's say you want add a new property address then also it works.

 obj2 = {name: "Maria", age: 17,address: "CA"}
 JSON.stringify(obj1) === JSON.stringify(obj2)// Returns false

Here is a link that you can find it helpful. Object comparison in JavaScript

Community
  • 1
  • 1
Saroj
  • 1,551
  • 2
  • 14
  • 30
0

This generic Object.prototype.compare() method might be in help to you

Object.prototype.compare = function(o){
  var ok = Object.keys(this);
  return typeof o === "object" && ok.length === Object.keys(o).length ? ok.every(k => this[k] === o[k]) : false;
};

var oa = [{name: "James", age: 17}, {name: "Maria", age: 17},{name: "James", age: 17}];

console.log(oa[0].compare(oa[1]));
console.log(oa[0].compare(oa[2]));
console.log(oa[1].compare(oa[1]));
Redu
  • 25,060
  • 6
  • 56
  • 76
0

There You go. Just in toCompare* = list*.map(i => i.XXX) for XXX set what You are going to compare. For now it is set i.id witch compares id of each item in the array listA of objects with id in the array listB of objects

const compareArrayObjects = (listA, listB) => {
    const toCompareA = listA.map(i => i.id)
    const toCompareB = listB.map(i => i.id)
    let list = []
    toCompareB.some(i => {
        const index = toCompareA.indexOf(i)
        if(index >= 0) {
            const item = listA[index]
            item.id = 'matching id ' + item.id
            // if you want to have list A with indicated what is matching
            // list = [
            //   ...listA.slice(0, index),
            //   item,
            //   ...listA.slice(index + 1)
            // ]
            // or you want to have a new array with matching items
            list.push(item.id)
            }
        })
    return list
}

console.log(compareArrayObjects(listAofObjects, listBofObjects))

// to have returned list with no repetitions:

const delRepSort = (listA, listB) => {
  const toCompareA = listA.map(i => i.id)
  const toCompareB = listB.map(i => i.id)
  let list = []
  toCompareA.some(i => {
    const index = toCompareB.indexOf(i)
    item = listA[toCompareA.indexOf(i)]
    if(index < 0) {
      list.push(item)
    }
  })
  return list
}