-1

I am trying to remove object if object matches, I dont want to compare any object key, I just want to compare whole object with array of object, and if it matches, then I have to remove that object from original array.

let originalArray = [
                {name: 'abc', country: 'eng'},
                {name: 'xyz', country: 'ind'},
                {name: 'pqr', country: 'us'}
            ]

    let objectToBeRemove = [
                {name: 'pqr', country: 'us'}
            ]

console.log(originalArray);

Expected output:

 [
                    {name: 'abc', country: 'eng'},
                    {name: 'xyz', country: 'ind'}
]

I am not able to figure out how can I compare object, I can do it by id or any key, but I am making generic thing, so may be in few cases ID is not present, that's why I want to compare object

AT82
  • 71,416
  • 24
  • 140
  • 167
techguy
  • 71
  • 8
  • @Nitheesh I have array of object – techguy Nov 17 '21 at 10:48
  • Does this answer your question? [How to get the difference between two arrays of objects in JavaScript](https://stackoverflow.com/questions/21987909/how-to-get-the-difference-between-two-arrays-of-objects-in-javascript) – slashroot Nov 17 '21 at 10:52
  • @slashroot no, I know the difference between those array, I just want to remove that different object from the original array – techguy Nov 17 '21 at 10:53

4 Answers4

3

One way is to use Array#filter with Array#some + JSON.stringify() for comparison.

Note that Array#filter returns a new array. So the variable needs to be reassigned.

let originalArray = [
  {name: 'abc', country: 'eng'},
  {name: 'xyz', country: 'ind'},
  {name: 'pqr', country: 'us'}
];

let objectToBeRemove = [
  {name: 'pqr', country: 'us'}
];

originalArray = originalArray.filter(obj => 
  objectToBeRemove.some(objToRemove => 
    JSON.stringify(objToRemove) !== JSON.stringify(obj)
  )
);

console.log(originalArray);

Note: Using JSON.stringify() is a little primitive for object comparison in my opinion. For instance the check would fail if the properties are in different order {country: 'us', name: 'pqr'}. Better way would be to do a deep comparison. For eg. using _.isEqual from loadash library. See here for more info.

Using loadash

let originalArray = [
  {name: 'abc', country: 'eng'},
  {name: 'xyz', country: 'ind'},
  {name: 'pqr', country: 'us'}
];

let objectToBeRemove = [
  {country: 'us', name: 'pqr'}
];

originalArray = originalArray.filter(obj => 
  objectToBeRemove.some(objToRemove => 
    !_.isEqual(objToRemove, obj)
  )
);

console.log(originalArray);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
ruth
  • 29,535
  • 4
  • 30
  • 57
0

You can use "filter" function to do that: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

var myArr = [{name: "toto"}, {name: "tata"}, {name: "tutu"}];
var toRemove = {name: "tata"};

let ret = myArr.filter(function(el) {
  return el.name != toRemove.name;
});

Or for removing multiple items:

var myArr = [{name: "toto"}, {name: "tata"}, {name: "tutu"}];
var toRemove = [{name: "tata"}, {name: "tutu"}];

for (var i = 0; i < toRemove.length; i++) {
  let index = myArr.findIndex((el) => { // Search item index to remove
    return el.name == toRemove[i].name;
  }); 

  if (index != -1) myArr.splice(index, 1); // Remove item in found index
}
Zatrof
  • 29
  • 4
0

To compare the equality of the complete object you could use the JSON.stringify() method.

Example;

JSON.stringfy(originalArray[i]) === JSON.stringfy(objectToBeRemove)
Mehmet YILMAZ
  • 111
  • 1
  • 6
0

try this

let originalArray = [
      { name: 'abc', country: 'eng' },
      { name: 'xyz', country: 'ind' },
      { name: 'pqr', country: 'us' }
    ];
    let objectToBeRemove = [
      { name: 'pqr', country: 'us' }
    ];
    var finalResult = originalArray.filter(function(objFromA) {
      return !objectToBeRemove.find(function(objFromB) {
        return objFromA.name === objFromB.name
      })
    });
    console.log('???',finalResult);
divyraj
  • 191
  • 2
  • 14
  • OP states _"compare whole object"_. At the moment the answer only compares `name` property. – ruth Nov 17 '21 at 11:06