Maniraj Murugan
Maniraj Murugan

Reputation: 9084

Remove the deleted values from array

I have two arrays such as,

Array 1:

    deletedValuesfromArray =
     [
        { "property_name": "Property three", "property_type": 4, "property_required": true, "property_origin": 2 }, 
        { "property_name": "rstywrtre", "property_type": 3, "property_required": true, "property_origin": 1 }
     ]

Array 2:

  normalArray =
   [
    { "property_name": "Property one", "property_type": 4, "property_required": true, "property_origin": 1 },
     { "property_name": "Property two", "property_type": 5, "property_required": true, "property_origin": 1 }, 
     { "property_name": "Property three", "property_type": 4, "property_required": true, "property_origin": 2 }, 
     { "property_name": "rstywrtre", "property_type": 3, "property_required": true, "property_origin": 1 }
   ]

I would like to compare both arrays and filter the array to get the new one after removing the deletedValuesfromArray (Array 1).

For which i have tried the following,

let newArray = this.normalArray.filter(function (val) {
  return this.deletedValuesfromArray.indexOf(val) == -1;
});
console.log(newArray);

But it doesn't works..

Expected Output is,

New Array

   [
     { "property_name": "Property one", "property_type": 4, "property_required": true, "property_origin": 1 },
     { "property_name": "Property two", "property_type": 5, "property_required": true, "property_origin": 1 }
   ]

Stackblitz that i have tried

The values also will not be unique always it may have a complete duplicate of any object.

How to compare and remove the deleted values from the normal array and get the newarray?

Upvotes: 0

Views: 128

Answers (5)

Pratik Rathi
Pratik Rathi

Reputation: 86

This might also help you.

function display()
{
  let newArray = this.normalArray.filter(function (val) {
  count = 0;
  var flag = true;
  return this.deletedValuesfromArray.find(function(item, i){
  count++;

     if(val.property_name == item.property_name && val.property_type == item.property_type && val.property_required == item.property_required && val.property_origin == item.property_origin ){
       flag =  false;
     }
     if(count == deletedValuesfromArray.length) {
     return flag;
     }
  });

}); }

Upvotes: 0

SeleM
SeleM

Reputation: 9638

You can proceed with ES6 operators (filter and some) :

 ngOnInit() {
    let newArray = this.normalArray.filter( (val) => {
      return !this.deletedValuesfromArray.some((newval) => {
       return JSON.stringify(newval) === JSON.stringify(val) // check if two objects are the same
      });
    });
    console.log(newArray);
  }

Upvotes: 0

Artyom Amiryan
Artyom Amiryan

Reputation: 2966

one of simple options here you can use JSON.stringify, which will turn objects into strings and compare them as strings, because 2 objects never equal each other by value since it compares them by reference, make sure that your objects keys order are the same

const deletedValuesfromArray =
     [
        { "property_name": "Property three", "property_type": 4, "property_required": true, "property_origin": 2 }, 
        { "property_name": "rstywrtre", "property_type": 3, "property_required": true, "property_origin": 1 }
     ]

  const normalArray =
   [
    { "property_name": "Property one", "property_type": 4, "property_required": true, "property_origin": 1 },
     { "property_name": "Property two", "property_type": 5, "property_required": true, "property_origin": 1 }, 
     { "property_name": "Property three", "property_type": 4, "property_required": true, "property_origin": 2 }, 
     { "property_name": "rstywrtre", "property_type": 3, "property_required": true, "property_origin": 1 }
   ]

const result = normalArray.filter(obj => !deletedValuesfromArray.find(o => JSON.stringify(o) == JSON.stringify(obj)));

console.log(result);

Upvotes: 0

Chandru
Chandru

Reputation: 11184

Using lodash :-

npm install --save lodash


import * as _ from "lodash";

var newArray = _.differenceWith(this.normalArray, this.deletedValuesfromArray, _.isEqual);
console.log("newArray", newArray);

Upvotes: 0

shkaper
shkaper

Reputation: 4998

Your filter predicate doesn't work because you can't just compare an object from normalArray to an object from deletedValuesfromArray:

indexOf() compares searchElement to elements of the Array using strict equality (the same method used by the === or triple-equals operator).

-- Array.indexOf()

In other words,

{ "property_name": "Property one", "property_type": 4, "property_required": true, "property_origin": 1 } === { "property_name": "Property one", "property_type": 4, "property_required": true, "property_origin": 1 }
// > false

To make the filter work, you need to implement a comparison function. See How to determine equality for two JavaScript objects? for some ideas.

Upvotes: 1

Related Questions