【问题标题】:Compare two array of objects based on only matching properties仅基于匹配的属性比较两个对象数组
【发布时间】:2021-12-27 15:16:57
【问题描述】:

我有两个对象数组

    this.originalData = [
        {age: 27, name: "sachin", id: 1, sex: "male", dept: "angular"},
        {age: 22, name: "pooja", id: 2, sex: "female", dept: "java"},
        {age: 50, name: "john", id: 3, sex: "male", dept: "sales"}
    ]

    this.updatedData = [
        {id: 1, name: "sachin", age: 25, sex: "male"},
        {id: 2, name: "pooja", age: 22, sex: "female"},
        {id: 3, name: "john", age: 50, sex: "male"}
    ]       

我们可以看到,两个数组中的 ordernumber of properties 是不同的。在这里,我如何才能只对匹配的属性进行比较,无论它是否被更改。在上面的示例中,我需要从updatedData 中获取带有id 1 的对象,因为与originalData 相比,age 属性从27 更改为25。不匹配的属性可以忽略。

我尝试如下,但由于差异而无法正常工作

  if(JSON.stringify(this.updatedData) !== JSON.stringify(this.originalData)) {
      console.log('changed!');
  }

请提出建议。谢谢。

【问题讨论】:

标签: javascript


【解决方案1】:
        const originalData = [
            { age: 27, name: "sachin", id: 1, sex: "male", dept: "angular" },
            { age: 22, name: "pooja", id: 2, sex: "female", dept: "java" },
            { age: 50, name: "john", id: 3, sex: "male", dept: "sales" }
        ]

        const updatedData = [
            { id: 1, name: "sachin", age: 25, sex: "male" },
            { id: 2, name: "pooja", age: 22, sex: "female" },
            { id: 3, name: "john", age: 50, sex: "male" }
        ]

        const output = updatedData.filter(uData => {
            const commonDataRow = originalData.find(oData => oData.id === uData.id); /** filter out common entry between both arrays based on id */
            const allPropertiesAreSame = Object.keys(commonDataRow).every(oDataEntry => /** iterate through the list of properties of common entry */
                /**
                 * if the updatedData object has the properties, check if the values are same for both the objects and return appropriately,
                 * else ignore the property (by returning false)
                 */
                uData.hasOwnProperty(oDataEntry) ?
                    commonDataRow[oDataEntry] === uData[oDataEntry] : true
            );
            /** if all properties are same, return false (as we desire to filter out those entries which have at least one unmatching property) */
            return !allPropertiesAreSame;
        });

        /** 
         * print the output 
         * the output will contain the list of objects matching the above criteria
         * format it to get the list of ID's
         */
        console.log(output);

【讨论】:

    【解决方案2】:

    以下代码-sn-p 为您提供所有数据已更改的项目(忽略其中任何一个都不存在的键)。

        let originalData = [
            {age: 27, name: "sachin", id: 1, sex: "male", dept: "angular"},
            {age: 22, name: "pooja", id: 2, sex: "female", dept: "java"},
            {age: 50, name: "john", id: 3, sex: "male", dept: "sales"}
        ];
    
        let updatedData = [
            {id: 1, name: "sachin", age: 25, sex: "male"},
            {id: 2, name: "pooja", age: 22, sex: "female"},
            {id: 3, name: "john", age: 50, sex: "male"}
        ];
        
        let changedList = [];
        
        originalData.map((item)=> {
          let temp = (updatedData.filter((x) => item.id === x.id ))[0];
          let same = true;
          if((item.age && temp.age) && (item.age !== temp.age)) {same = false}
          if((item.name && temp.name) && (item.name !== temp.name)) {same = false}
          if((item.sex && temp.sex) && (item.sex !== temp.sex)) {same = false}
          if((item.dept && temp.dept) && (item.dept !== temp.dept)) {same = false} 
          if(same === false) {changedList.push(item)};
          console.log(same);
        });
        
        console.log(changedList);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-04
      • 1970-01-01
      • 2016-04-07
      • 1970-01-01
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      • 2016-09-07
      相关资源
      最近更新 更多