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?
javascript arrays angular typescript filter
add a comment |
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?
javascript arrays angular typescript filter
add a comment |
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?
javascript arrays angular typescript filter
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?
javascript arrays angular typescript filter
javascript arrays angular typescript filter
edited Nov 14 '18 at 14:54
James Z
11.2k71935
11.2k71935
asked Nov 14 '18 at 13:22
Maniraj from KarurManiraj from Karur
1,0491134
1,0491134
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
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).
-- 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.
add a comment |
Using lodash :-
npm install --save lodash
import * as _ from "lodash";
var newArray = _.differenceWith(this.normalArray, this.deletedValuesfromArray, _.isEqual);
console.log("newArray", newArray);
I should not use any library or package in my angular project..
– Maniraj from Karur
Nov 14 '18 at 13:38
Try like this - stackoverflow.com/a/21988185/5362646
– Chandru
Nov 14 '18 at 13:42
FYI, lodash is swiss army knife of Javascript.
– devansvd
Nov 14 '18 at 13:53
add a comment |
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);
what if the order of keys is different in two same-looking objects?
– shkaper
Nov 14 '18 at 13:35
@shkaper the order is important in my example, I will update my answer
– Artyom Amiryan
Nov 14 '18 at 13:36
who can explain the downvote ?
– Artyom Amiryan
Nov 14 '18 at 16:07
I can explain the down vote. Using stringify is a terrible solution and the guy that made the question didn't say that he wants to check that the two objects are exactly identical. You should NEVER expect that the order of keys is the same.
– itsundefined
Nov 14 '18 at 20:50
1
@itsundefined from the question it is easy to get that he wants to check that the two objects are exactly identical. Almost all answers contain the same logic, as accepted answer too which marked by question creator, it means we got question right, and I noted that in my example order is important, so I don't see any reason for down vote, it seems you got question wrong and added down votes to answers which have upvote
– Artyom Amiryan
Nov 14 '18 at 21:02
|
show 2 more comments
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);
}
Here you're assuming thatproperty_name
values are unique. The OP never said that
– shkaper
Nov 14 '18 at 13:42
Yes this solution seems to be close to me but the thing is as @shkaper said the values may not be unique at all time..
– Maniraj from Karur
Nov 14 '18 at 13:58
@ManiRaj updated answer !
– selem mn
Nov 14 '18 at 14:26
3
this is potentially dangerous since object's property order is not guaranteed in JavaScript
– shkaper
Nov 14 '18 at 14:55
If you need an explanation about why this solution is terrible, please consider how much cpu time is wasted to stringify all those objects. You are also stringifying the same object way too many times with the way this loop works. You are also not taking into account that there is no spec in javascript that says that the order of object properties stays the same.
– itsundefined
Nov 14 '18 at 20:58
add a comment |
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;
}
});
});
}
please vote if this ans helps you
– Pratik Rathi
Nov 15 '18 at 6:16
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301255%2fremove-the-deleted-values-from-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
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).
-- 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.
add a comment |
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).
-- 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.
add a comment |
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).
-- 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.
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).
-- 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.
answered Nov 14 '18 at 13:31
shkapershkaper
1,2511814
1,2511814
add a comment |
add a comment |
Using lodash :-
npm install --save lodash
import * as _ from "lodash";
var newArray = _.differenceWith(this.normalArray, this.deletedValuesfromArray, _.isEqual);
console.log("newArray", newArray);
I should not use any library or package in my angular project..
– Maniraj from Karur
Nov 14 '18 at 13:38
Try like this - stackoverflow.com/a/21988185/5362646
– Chandru
Nov 14 '18 at 13:42
FYI, lodash is swiss army knife of Javascript.
– devansvd
Nov 14 '18 at 13:53
add a comment |
Using lodash :-
npm install --save lodash
import * as _ from "lodash";
var newArray = _.differenceWith(this.normalArray, this.deletedValuesfromArray, _.isEqual);
console.log("newArray", newArray);
I should not use any library or package in my angular project..
– Maniraj from Karur
Nov 14 '18 at 13:38
Try like this - stackoverflow.com/a/21988185/5362646
– Chandru
Nov 14 '18 at 13:42
FYI, lodash is swiss army knife of Javascript.
– devansvd
Nov 14 '18 at 13:53
add a comment |
Using lodash :-
npm install --save lodash
import * as _ from "lodash";
var newArray = _.differenceWith(this.normalArray, this.deletedValuesfromArray, _.isEqual);
console.log("newArray", newArray);
Using lodash :-
npm install --save lodash
import * as _ from "lodash";
var newArray = _.differenceWith(this.normalArray, this.deletedValuesfromArray, _.isEqual);
console.log("newArray", newArray);
answered Nov 14 '18 at 13:36
ChandruChandru
5,20721229
5,20721229
I should not use any library or package in my angular project..
– Maniraj from Karur
Nov 14 '18 at 13:38
Try like this - stackoverflow.com/a/21988185/5362646
– Chandru
Nov 14 '18 at 13:42
FYI, lodash is swiss army knife of Javascript.
– devansvd
Nov 14 '18 at 13:53
add a comment |
I should not use any library or package in my angular project..
– Maniraj from Karur
Nov 14 '18 at 13:38
Try like this - stackoverflow.com/a/21988185/5362646
– Chandru
Nov 14 '18 at 13:42
FYI, lodash is swiss army knife of Javascript.
– devansvd
Nov 14 '18 at 13:53
I should not use any library or package in my angular project..
– Maniraj from Karur
Nov 14 '18 at 13:38
I should not use any library or package in my angular project..
– Maniraj from Karur
Nov 14 '18 at 13:38
Try like this - stackoverflow.com/a/21988185/5362646
– Chandru
Nov 14 '18 at 13:42
Try like this - stackoverflow.com/a/21988185/5362646
– Chandru
Nov 14 '18 at 13:42
FYI, lodash is swiss army knife of Javascript.
– devansvd
Nov 14 '18 at 13:53
FYI, lodash is swiss army knife of Javascript.
– devansvd
Nov 14 '18 at 13:53
add a comment |
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);
what if the order of keys is different in two same-looking objects?
– shkaper
Nov 14 '18 at 13:35
@shkaper the order is important in my example, I will update my answer
– Artyom Amiryan
Nov 14 '18 at 13:36
who can explain the downvote ?
– Artyom Amiryan
Nov 14 '18 at 16:07
I can explain the down vote. Using stringify is a terrible solution and the guy that made the question didn't say that he wants to check that the two objects are exactly identical. You should NEVER expect that the order of keys is the same.
– itsundefined
Nov 14 '18 at 20:50
1
@itsundefined from the question it is easy to get that he wants to check that the two objects are exactly identical. Almost all answers contain the same logic, as accepted answer too which marked by question creator, it means we got question right, and I noted that in my example order is important, so I don't see any reason for down vote, it seems you got question wrong and added down votes to answers which have upvote
– Artyom Amiryan
Nov 14 '18 at 21:02
|
show 2 more comments
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);
what if the order of keys is different in two same-looking objects?
– shkaper
Nov 14 '18 at 13:35
@shkaper the order is important in my example, I will update my answer
– Artyom Amiryan
Nov 14 '18 at 13:36
who can explain the downvote ?
– Artyom Amiryan
Nov 14 '18 at 16:07
I can explain the down vote. Using stringify is a terrible solution and the guy that made the question didn't say that he wants to check that the two objects are exactly identical. You should NEVER expect that the order of keys is the same.
– itsundefined
Nov 14 '18 at 20:50
1
@itsundefined from the question it is easy to get that he wants to check that the two objects are exactly identical. Almost all answers contain the same logic, as accepted answer too which marked by question creator, it means we got question right, and I noted that in my example order is important, so I don't see any reason for down vote, it seems you got question wrong and added down votes to answers which have upvote
– Artyom Amiryan
Nov 14 '18 at 21:02
|
show 2 more comments
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);
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);
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);
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);
edited Nov 14 '18 at 13:37
answered Nov 14 '18 at 13:34
Artyom AmiryanArtyom Amiryan
1,9371214
1,9371214
what if the order of keys is different in two same-looking objects?
– shkaper
Nov 14 '18 at 13:35
@shkaper the order is important in my example, I will update my answer
– Artyom Amiryan
Nov 14 '18 at 13:36
who can explain the downvote ?
– Artyom Amiryan
Nov 14 '18 at 16:07
I can explain the down vote. Using stringify is a terrible solution and the guy that made the question didn't say that he wants to check that the two objects are exactly identical. You should NEVER expect that the order of keys is the same.
– itsundefined
Nov 14 '18 at 20:50
1
@itsundefined from the question it is easy to get that he wants to check that the two objects are exactly identical. Almost all answers contain the same logic, as accepted answer too which marked by question creator, it means we got question right, and I noted that in my example order is important, so I don't see any reason for down vote, it seems you got question wrong and added down votes to answers which have upvote
– Artyom Amiryan
Nov 14 '18 at 21:02
|
show 2 more comments
what if the order of keys is different in two same-looking objects?
– shkaper
Nov 14 '18 at 13:35
@shkaper the order is important in my example, I will update my answer
– Artyom Amiryan
Nov 14 '18 at 13:36
who can explain the downvote ?
– Artyom Amiryan
Nov 14 '18 at 16:07
I can explain the down vote. Using stringify is a terrible solution and the guy that made the question didn't say that he wants to check that the two objects are exactly identical. You should NEVER expect that the order of keys is the same.
– itsundefined
Nov 14 '18 at 20:50
1
@itsundefined from the question it is easy to get that he wants to check that the two objects are exactly identical. Almost all answers contain the same logic, as accepted answer too which marked by question creator, it means we got question right, and I noted that in my example order is important, so I don't see any reason for down vote, it seems you got question wrong and added down votes to answers which have upvote
– Artyom Amiryan
Nov 14 '18 at 21:02
what if the order of keys is different in two same-looking objects?
– shkaper
Nov 14 '18 at 13:35
what if the order of keys is different in two same-looking objects?
– shkaper
Nov 14 '18 at 13:35
@shkaper the order is important in my example, I will update my answer
– Artyom Amiryan
Nov 14 '18 at 13:36
@shkaper the order is important in my example, I will update my answer
– Artyom Amiryan
Nov 14 '18 at 13:36
who can explain the downvote ?
– Artyom Amiryan
Nov 14 '18 at 16:07
who can explain the downvote ?
– Artyom Amiryan
Nov 14 '18 at 16:07
I can explain the down vote. Using stringify is a terrible solution and the guy that made the question didn't say that he wants to check that the two objects are exactly identical. You should NEVER expect that the order of keys is the same.
– itsundefined
Nov 14 '18 at 20:50
I can explain the down vote. Using stringify is a terrible solution and the guy that made the question didn't say that he wants to check that the two objects are exactly identical. You should NEVER expect that the order of keys is the same.
– itsundefined
Nov 14 '18 at 20:50
1
1
@itsundefined from the question it is easy to get that he wants to check that the two objects are exactly identical. Almost all answers contain the same logic, as accepted answer too which marked by question creator, it means we got question right, and I noted that in my example order is important, so I don't see any reason for down vote, it seems you got question wrong and added down votes to answers which have upvote
– Artyom Amiryan
Nov 14 '18 at 21:02
@itsundefined from the question it is easy to get that he wants to check that the two objects are exactly identical. Almost all answers contain the same logic, as accepted answer too which marked by question creator, it means we got question right, and I noted that in my example order is important, so I don't see any reason for down vote, it seems you got question wrong and added down votes to answers which have upvote
– Artyom Amiryan
Nov 14 '18 at 21:02
|
show 2 more comments
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);
}
Here you're assuming thatproperty_name
values are unique. The OP never said that
– shkaper
Nov 14 '18 at 13:42
Yes this solution seems to be close to me but the thing is as @shkaper said the values may not be unique at all time..
– Maniraj from Karur
Nov 14 '18 at 13:58
@ManiRaj updated answer !
– selem mn
Nov 14 '18 at 14:26
3
this is potentially dangerous since object's property order is not guaranteed in JavaScript
– shkaper
Nov 14 '18 at 14:55
If you need an explanation about why this solution is terrible, please consider how much cpu time is wasted to stringify all those objects. You are also stringifying the same object way too many times with the way this loop works. You are also not taking into account that there is no spec in javascript that says that the order of object properties stays the same.
– itsundefined
Nov 14 '18 at 20:58
add a comment |
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);
}
Here you're assuming thatproperty_name
values are unique. The OP never said that
– shkaper
Nov 14 '18 at 13:42
Yes this solution seems to be close to me but the thing is as @shkaper said the values may not be unique at all time..
– Maniraj from Karur
Nov 14 '18 at 13:58
@ManiRaj updated answer !
– selem mn
Nov 14 '18 at 14:26
3
this is potentially dangerous since object's property order is not guaranteed in JavaScript
– shkaper
Nov 14 '18 at 14:55
If you need an explanation about why this solution is terrible, please consider how much cpu time is wasted to stringify all those objects. You are also stringifying the same object way too many times with the way this loop works. You are also not taking into account that there is no spec in javascript that says that the order of object properties stays the same.
– itsundefined
Nov 14 '18 at 20:58
add a comment |
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);
}
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);
}
edited Nov 14 '18 at 14:25
answered Nov 14 '18 at 13:33
selem mnselem mn
5,07541939
5,07541939
Here you're assuming thatproperty_name
values are unique. The OP never said that
– shkaper
Nov 14 '18 at 13:42
Yes this solution seems to be close to me but the thing is as @shkaper said the values may not be unique at all time..
– Maniraj from Karur
Nov 14 '18 at 13:58
@ManiRaj updated answer !
– selem mn
Nov 14 '18 at 14:26
3
this is potentially dangerous since object's property order is not guaranteed in JavaScript
– shkaper
Nov 14 '18 at 14:55
If you need an explanation about why this solution is terrible, please consider how much cpu time is wasted to stringify all those objects. You are also stringifying the same object way too many times with the way this loop works. You are also not taking into account that there is no spec in javascript that says that the order of object properties stays the same.
– itsundefined
Nov 14 '18 at 20:58
add a comment |
Here you're assuming thatproperty_name
values are unique. The OP never said that
– shkaper
Nov 14 '18 at 13:42
Yes this solution seems to be close to me but the thing is as @shkaper said the values may not be unique at all time..
– Maniraj from Karur
Nov 14 '18 at 13:58
@ManiRaj updated answer !
– selem mn
Nov 14 '18 at 14:26
3
this is potentially dangerous since object's property order is not guaranteed in JavaScript
– shkaper
Nov 14 '18 at 14:55
If you need an explanation about why this solution is terrible, please consider how much cpu time is wasted to stringify all those objects. You are also stringifying the same object way too many times with the way this loop works. You are also not taking into account that there is no spec in javascript that says that the order of object properties stays the same.
– itsundefined
Nov 14 '18 at 20:58
Here you're assuming that
property_name
values are unique. The OP never said that– shkaper
Nov 14 '18 at 13:42
Here you're assuming that
property_name
values are unique. The OP never said that– shkaper
Nov 14 '18 at 13:42
Yes this solution seems to be close to me but the thing is as @shkaper said the values may not be unique at all time..
– Maniraj from Karur
Nov 14 '18 at 13:58
Yes this solution seems to be close to me but the thing is as @shkaper said the values may not be unique at all time..
– Maniraj from Karur
Nov 14 '18 at 13:58
@ManiRaj updated answer !
– selem mn
Nov 14 '18 at 14:26
@ManiRaj updated answer !
– selem mn
Nov 14 '18 at 14:26
3
3
this is potentially dangerous since object's property order is not guaranteed in JavaScript
– shkaper
Nov 14 '18 at 14:55
this is potentially dangerous since object's property order is not guaranteed in JavaScript
– shkaper
Nov 14 '18 at 14:55
If you need an explanation about why this solution is terrible, please consider how much cpu time is wasted to stringify all those objects. You are also stringifying the same object way too many times with the way this loop works. You are also not taking into account that there is no spec in javascript that says that the order of object properties stays the same.
– itsundefined
Nov 14 '18 at 20:58
If you need an explanation about why this solution is terrible, please consider how much cpu time is wasted to stringify all those objects. You are also stringifying the same object way too many times with the way this loop works. You are also not taking into account that there is no spec in javascript that says that the order of object properties stays the same.
– itsundefined
Nov 14 '18 at 20:58
add a comment |
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;
}
});
});
}
please vote if this ans helps you
– Pratik Rathi
Nov 15 '18 at 6:16
add a comment |
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;
}
});
});
}
please vote if this ans helps you
– Pratik Rathi
Nov 15 '18 at 6:16
add a comment |
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;
}
});
});
}
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;
}
});
});
}
answered Nov 15 '18 at 6:15
Pratik RathiPratik Rathi
66
66
please vote if this ans helps you
– Pratik Rathi
Nov 15 '18 at 6:16
add a comment |
please vote if this ans helps you
– Pratik Rathi
Nov 15 '18 at 6:16
please vote if this ans helps you
– Pratik Rathi
Nov 15 '18 at 6:16
please vote if this ans helps you
– Pratik Rathi
Nov 15 '18 at 6:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301255%2fremove-the-deleted-values-from-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown