Home > Software engineering >  Compare two arrays of objects, and remove if object value is equal
Compare two arrays of objects, and remove if object value is equal

Time:03-02

I've tried modifying some of the similar solutions on here but I keep getting stuck, I believe I have part of this figured out however, the main caveat is that:

Some of the objects have extra keys, which renders my object comparison logic useless.

I am trying to compare two arrays of objects. One array is the original array, and the other array contains the items I want deleted from the original array. However there's one extra issue in that the second array contains extra keys, so my comparison logic doesn't work.

An example would make this easier, let's say I have the following two arrays:

const originalArray = [{id: 1, name: "darnell"}, {id: 2, name: "funboi"}, 
{id: 3, name: "jackson5"}, {id: 4, name: "zelensky"}];

const itemsToBeRemoved = [{id: 2, name: "funboi", extraProperty: "something"}, 
{id: 4, name: "zelensky", extraProperty: "somethingelse"}];

after running the logic, my final output should be this array:

[{id: 1, name: "darnell"}, {id: 3, name: "jackson5"}]

And here's the current code / logic that I have, which compares but doesn't handle the extra keys. How should I handle this? Thank you in advance.

const prepareArray = (arr) => {
  return arr.map((el) => {
    if (typeof el === "object" && el !== null) {
      return JSON.stringify(el);
    } else {
      return el;
    }
  });
};

const convertJSON = (arr) => {
  return arr.map((el) => {
    return JSON.parse(el);
  });
};

const compareArrays = (arr1, arr2) => {
  const currentArray = [...prepareArray(arr1)];
  const deletedItems = [...prepareArray(arr2)];
  const compared = currentArray.filter((el) => deletedItems.indexOf(el) === -1);
  return convertJSON(compared);
};

CodePudding user response:

How about using filter and some? You can extend the filter condition on select properties using &&.

const originalArray = [
  { id: 1, name: 'darnell' },
  { id: 2, name: 'funboi' },
  { id: 3, name: 'jackson5' },
  { id: 4, name: 'zelensky' },
];
const itemsToBeRemoved = [
  { id: 2, name: 'funboi', extraProperty: 'something' },
  { id: 4, name: 'zelensky', extraProperty: 'somethingelse' },
];
console.log(
  originalArray.filter(item => !itemsToBeRemoved.some(itemToBeRemoved => itemToBeRemoved.id === item.id))
)

Or you can generalise it as well.

const originalArray = [
  { id: 1, name: 'darnell' },
  { id: 2, name: 'funboi' },
  { id: 3, name: 'jackson5' },
  { id: 4, name: 'zelensky' },
];
const itemsToBeRemoved = [
  { id: 2, name: 'funboi', extraProperty: 'something' },
  { id: 4, name: 'zelensky', extraProperty: 'somethingelse' },
];
function filterIfSubset(originalArray, itemsToBeRemoved) {
  const filteredArray = [];
  for (let i = 0; i < originalArray.length; i  ) {
    let isSubset = false;
    for (let j = 0; j < itemsToBeRemoved.length; j  ) {
      // check if whole object is a subset of the object in itemsToBeRemoved
      if (Object.keys(originalArray[i]).every(key => originalArray[i][key] === itemsToBeRemoved[j][key])) {
        isSubset = true;
      }
    }
    if (!isSubset) {
      filteredArray.push(originalArray[i]);
    }
  }
  return filteredArray;
}
console.log(filterIfSubset(originalArray, itemsToBeRemoved));

Another simpler variation of the second approach:

const originalArray = [
  { id: 1, name: 'darnell' },
  { id: 2, name: 'funboi' },
  { id: 3, name: 'jackson5' },
  { id: 4, name: 'zelensky' },
];
const itemsToBeRemoved = [
  { id: 2, name: 'funboi', extraProperty: 'something' },
  { id: 4, name: 'zelensky', extraProperty: 'somethingelse' },
];
const removeSubsetObjectsIfExists = (originalArray, itemsToBeRemoved) => {
  return originalArray.filter(item => {
    const isSubset = itemsToBeRemoved.some(itemToBeRemoved => {
      return Object.keys(item).every(key => {
        return item[key] === itemToBeRemoved[key];
      });
    });
    return !isSubset;
  });
}
console.log(removeSubsetObjectsIfExists(originalArray, itemsToBeRemoved));

CodePudding user response:

The example below is a reusable function, the third parameter is the key to which you compare values from both arrays.

Details are commented in example

const arr=[{id:1,name:"darnell"},{id:2,name:"funboi"},{id:3,name:"jackson5"},{id:4,name:"zelensky"}],del=[{id:2,name:"funboi",extraProperty:"something"},{id:4,name:"zelensky",extraProperty:"somethingelse"}];

/** Compare arrayA vs. delArray by a given key's value.
--- ex. key = 'id'
**/
function deleteByKey(arrayA, delArray, key) {
  /* Get an array of only the values of the given key from delArray
  --- ex. delList = [1, 2, 3, 4]
  */
  const delList = delArray.map(obj => obj[key]);
  
  /* On every object of arrayA compare delList values vs 
 current object's key's value
  --- ex. current obj[id] = 2
  --- [1, 2, 3, 4].includes(obj[id])
  Any match returns an empty array and non-matches are returned 
  in it's own array.
  --- ex. ? [] : [obj]
  The final return is a flattened array of the non-matching objects
  */
  return arrayA.flatMap(obj => delList.includes(obj[key]) ? [] : [obj]);  
};

console.log(deleteByKey(arr, del, 'id'));

  • Related