【问题标题】:Compare array of objects for differences比较对象数组的差异
【发布时间】:2016-10-19 07:10:57
【问题描述】:

我需要一些帮助来检查已更改的对象数组。 例如,我有一个对象数组,如果他的长度或属性值发生更改,我需要检测更改后的情况。如果对象发生了任何更改,它会给我一个布尔响应。

有对此有用的 lodash 函数吗?

数组对象示例:

[{name: "James", age: 17}, {name: "Maria", age: 17},...]

【问题讨论】:

  • 谷歌搜索“深度相等的 javascript 对象”。

标签: javascript lodash


【解决方案1】:

你去吧。只需在 toCompare* = list*.map(i => i.XXX) for XXX 中设置您要比较的内容。现在设置 i.id 女巫将对象数组 listA 中每个项目的 id 与对象数组 listB 中的 id 进行比较

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

// 没有重复的返回列表:

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
}

【讨论】:

    【解决方案2】:

    这个通用的Object.prototype.compare() 方法可能对你有帮助

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

    【讨论】:

      【解决方案3】:

      如果你想根据属性检查两个对象,你可以这样做。

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

      将obj2的name属性修改为“James”

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

      假设您想添加一个新的物业地址,那么它也可以工作。

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

      这里有一个链接,您会发现它很有帮助。 Object comparison in JavaScript

      【讨论】:

        【解决方案4】:

        Lodash 的 isEqual 函数在这种情况下可以帮助您:

        _.isEqual(firstArray, secondArray)
        

        注意顺序对于数组很重要:

        _.isEqual([1, 2], [2, 1]) // false
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-08-18
          • 2018-01-20
          • 2023-03-27
          • 1970-01-01
          • 1970-01-01
          • 2020-09-18
          • 2020-01-25
          • 1970-01-01
          相关资源
          最近更新 更多