繁体   English   中英

仅基于匹配的属性比较两个对象数组

[英]Compare two array of objects based on only matching properties

我有两个对象数组

    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"}
    ]       

正如我们所见,arrays 中属性的顺序数量是不同的。 在这里,我如何才能只对匹配的属性进行比较,无论它是否被更改。 在上面的示例中,我需要从 updatedData 获取id 1updatedData ,因为与originalData相比, age属性从27更改为25 不匹配的属性可以忽略。

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

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

请建议。 谢谢。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM