Reputation: 9084
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
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
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
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
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
Reputation: 4998
Your filter
predicate doesn't work because you can't just compare an object from normalArray
to an object from deletedValuesfromArray
:
indexOf()
comparessearchElement
to elements of theArray
using strict equality (the same method used by the === or triple-equals operator).
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