3

There are two array :

arr1 = [
        {name: "gunner", id: 639, label: "group"}, 
        {name: "Gunners", id: 1313, label: "channel", isPrivate: false}
       ]
arr2 = [
        {name: "gunner", id: 639, label: "group"}
       ]

Need to remove all the values from arr1 which exists in arr2 ?

Final arr1 will be, as arr2 already has {name: "gunner", id: 639, label: "group"}:

arr1 = [
        {name: "Gunners", id: 1313, label: "channel", isPrivate: false}
       ]

I tried this but doesn't work as expected.

arr1 = arr1.filter(function(val) {
  return arr2.indexOf(val) == -1;
});

4 Answers 4

2

If the property order is predictable, make a Set of the stringified items in arr2 first, then filter by whether the stringified item in arr1 exists in that set:

const arr1 = [
  {name: "gunner", id: 639, label: "group"}, 
  {name: "Gunners", id: 1313, label: "channel", isPrivate: false}
];
const arr2 = [
  {name: "gunner", id: 639, label: "group"}
];
const arr2Set = new Set(arr2.map(JSON.stringify));

const output = arr1.filter(obj => !arr2Set.has(JSON.stringify(obj)));
console.log(output);

If you don't want to or can't stringify, then iterate over each of the Object.entries of the object instead, and check that not every entry matches in the other array:

const arr1 = [
  {name: "gunner", id: 639, label: "group"}, 
  {name: "Gunners", id: 1313, label: "channel", isPrivate: false}
];
const arr2 = [
  {name: "gunner", id: 639, label: "group"}
];

const output = arr1.filter((obj1) => {
  const entries1 = Object.entries(obj1);
  return arr2.every(obj2 => (
    entries1.length !== Object.keys(obj2).length ||
    entries1.some(([key, val]) => obj2[key] !== val)
  ));
});

console.log(output);

Sign up to request clarification or add additional context in comments.

Comments

0

You should compare the id in those objects but not the object reference.

const idsInArr2 = arr2.map(val => val.id);
arr1 = arr1.filter(val => idsInArr2.indexOf(val.id) === -1);

Comments

0

You can use JSON.stringify to compare objects inside the loop:

var arr1 = [
  {name: "gunner", id: 639, label: "group"}, 
  {name: "Gunners", id: 1313, label: "channel", isPrivate: false}
];

var arr2 = [
  {name: "gunner", id: 639, label: "group"}
];

arr1 = arr1.filter(x => {
  for (var y of arr2) {
    if (JSON.stringify(x) !== JSON.stringify(y)) {
      return y;
    }
  }
});

console.log(arr1);

Comments

0

Try this:

const arr1 = [
        {name: "gunner", id: 639, label: "group"}, 
        {name: "Gunners", id: 1313, label: "channel", isPrivate: false}
       ]
const arr2 = [
        {name: "gunner", id: 639, label: "group"}
       ]

const arr3 = arr1.filter(function(objArr1) {
  return arr2.findIndex(objArr2=>objArr2.id===objArr1.id) === -1;
}); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.