Home > database >  Compare two array of objects based on only matching properties
Compare two array of objects based on only matching properties

Time:12-28

I have two array of objects

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

As we can see the order and number of properties is different in both the arrays. Here, how can I do the comparison for only matching properties whether any of it is changed. In the above example, I need to get the object with id 1 from updatedData as the age property is changed from 27 to 25 when compared with originalData. The properties which are not matching can be ignored.

I tried like below but it is not working due to the differences

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

Please suggest. Thanks.

CodePudding user response:

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

CodePudding user response:

The following code-snippet gives you all the items, where data has changed (ignoring keys which do not exist in either of them).

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

  • Related