Remove object from array based on array of some property of that object2019 Community Moderator ElectionIs...
Too soon for a plot twist?
Paper published similar to PhD thesis
Is it a Cyclops number? "Nobody" knows!
Python 3.6+ function to ask for a multiple-choice answer
Does the in-code argument passing conventions used on PDP-11's have a name?
Why is there an extra space when I type "ls" on the Desktop?
Short story about an infectious indestructible metal bar?
PTIJ: Aliyot for the deceased
PTiJ: How should animals pray?
Is divide-by-zero a security vulnerability?
What can I do if someone tampers with my SSH public key?
newcommand: Using one parameter as the default for the other
Can inspiration allow the Rogue to make a Sneak Attack?
Are small insurances worth it
Problems with rounding giving too many digits
When to use the term transposed instead of modulation?
Affine transformation of circular arc in 3D
What is the purpose of a disclaimer like "this is not legal advice"?
Do natural melee weapons (from racial traits) trigger Improved Divine Smite?
What is "desert glass" and what does it do to the PCs?
Naming Characters after Friends/Family
What's the difference between Compensation, Indemnity, and Reparations?
Error in TransformedField
Convert an array of objects to array of the objects' values
Remove object from array based on array of some property of that object
2019 Community Moderator ElectionIs Set.has() method O(1) and Array.indexOf O(n)?Detecting an undefined object propertyCreate ArrayList from arrayHow can I merge properties of two JavaScript objects dynamically?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?PHP: Delete an element from an arraySort array of objects by string property valueHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?Iterate through object properties
I have an array of objects (objList) that each has "id" property.
I have an array of strings (idsToRemove), representing IDs of the objects to remove from objList.
I find some solution but I fear it's slow, especially with the large list of objects with lots of properties.
Is there more efficient way to do this?
var idsToRemove = ["3", "1"];
var objList = [{
id: "1",
name: "aaa"
},
{
id: "2",
name: "bbb"
},
{
id: "3",
name: "ccc"
}
];
for (var i = 0, len = idsToRemove.length; i < len; i++) {
objList = objList.filter(o => o.id != idsToRemove[i]);
}
console.log(objList);
javascript arrays performance
add a comment |
I have an array of objects (objList) that each has "id" property.
I have an array of strings (idsToRemove), representing IDs of the objects to remove from objList.
I find some solution but I fear it's slow, especially with the large list of objects with lots of properties.
Is there more efficient way to do this?
var idsToRemove = ["3", "1"];
var objList = [{
id: "1",
name: "aaa"
},
{
id: "2",
name: "bbb"
},
{
id: "3",
name: "ccc"
}
];
for (var i = 0, len = idsToRemove.length; i < len; i++) {
objList = objList.filter(o => o.id != idsToRemove[i]);
}
console.log(objList);
javascript arrays performance
add a comment |
I have an array of objects (objList) that each has "id" property.
I have an array of strings (idsToRemove), representing IDs of the objects to remove from objList.
I find some solution but I fear it's slow, especially with the large list of objects with lots of properties.
Is there more efficient way to do this?
var idsToRemove = ["3", "1"];
var objList = [{
id: "1",
name: "aaa"
},
{
id: "2",
name: "bbb"
},
{
id: "3",
name: "ccc"
}
];
for (var i = 0, len = idsToRemove.length; i < len; i++) {
objList = objList.filter(o => o.id != idsToRemove[i]);
}
console.log(objList);
javascript arrays performance
I have an array of objects (objList) that each has "id" property.
I have an array of strings (idsToRemove), representing IDs of the objects to remove from objList.
I find some solution but I fear it's slow, especially with the large list of objects with lots of properties.
Is there more efficient way to do this?
var idsToRemove = ["3", "1"];
var objList = [{
id: "1",
name: "aaa"
},
{
id: "2",
name: "bbb"
},
{
id: "3",
name: "ccc"
}
];
for (var i = 0, len = idsToRemove.length; i < len; i++) {
objList = objList.filter(o => o.id != idsToRemove[i]);
}
console.log(objList);
var idsToRemove = ["3", "1"];
var objList = [{
id: "1",
name: "aaa"
},
{
id: "2",
name: "bbb"
},
{
id: "3",
name: "ccc"
}
];
for (var i = 0, len = idsToRemove.length; i < len; i++) {
objList = objList.filter(o => o.id != idsToRemove[i]);
}
console.log(objList);
var idsToRemove = ["3", "1"];
var objList = [{
id: "1",
name: "aaa"
},
{
id: "2",
name: "bbb"
},
{
id: "3",
name: "ccc"
}
];
for (var i = 0, len = idsToRemove.length; i < len; i++) {
objList = objList.filter(o => o.id != idsToRemove[i]);
}
console.log(objList);
javascript arrays performance
javascript arrays performance
edited 19 hours ago
Uwe Keim
27.6k32132213
27.6k32132213
asked 21 hours ago
DaliborDalibor
490318
490318
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
Turn the idsToRemove
into a Set
so that you can use Set.prototype.has
(an O(1)
operation), and .filter
the objList
just once, so that the overall complexity is O(n)
(and you only iterate over the possibly-huge objList
once):
var idsToRemove = ["3", "1"];
var objList = [{
id: "1",
name: "aaa"
},
{
id: "2",
name: "bbb"
},
{
id: "3",
name: "ccc"
}
];
const set = new Set(idsToRemove);
const filtered = objList.filter(({ id }) => !set.has(id));
console.log(filtered);
Note that Array.prototype.includes
and Array.prototype.indexOf
operations are O(N)
, not O(1)
, so if you use them instead of a Set
, they may take significantly longer.
Not sure if the claim Set.has is O(1) is correct. Please provide an answer to this question if it is correct. stackoverflow.com/questions/55057200/…
– Charlie H
2 hours ago
Does it not depend on the size of the array? If the array is small, is it not better to through it than creating a new copy in memory then access it ?
– Grégory NEUT
51 secs ago
add a comment |
You can use Array.includes
which check if the given string exists in the given array and combine it with an Array.filter
.
const idsToRemove = ['3', '1'];
const objList = [{
id: '1',
name: 'aaa',
},
{
id: '2',
name: 'bbb',
},
{
id: '3',
name: 'ccc',
},
];
const filteredObjList = objList.filter(x => !idsToRemove.includes(x.id));
console.log(filteredObjList);
add a comment |
You don't need two nested iterators if you use a built-in lookup function
objList = objList.filter(o => idsToRemove.indexOf(o.id) < 0);
Documentation:
Array.prototype.indexOf()
Array.prototype.includes()
add a comment |
Simply use Array.filter()
const idsToRemove = ['3', '1'];
const objList = [{
id: '1',
name: 'aaa',
},
{
id: '2',
name: 'bbb',
},
{
id: '3',
name: 'ccc',
}
];
const res = objList.filter(value => !idsToRemove.includes(value.id));
console.log("result",res);
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%2f55041973%2fremove-object-from-array-based-on-array-of-some-property-of-that-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Turn the idsToRemove
into a Set
so that you can use Set.prototype.has
(an O(1)
operation), and .filter
the objList
just once, so that the overall complexity is O(n)
(and you only iterate over the possibly-huge objList
once):
var idsToRemove = ["3", "1"];
var objList = [{
id: "1",
name: "aaa"
},
{
id: "2",
name: "bbb"
},
{
id: "3",
name: "ccc"
}
];
const set = new Set(idsToRemove);
const filtered = objList.filter(({ id }) => !set.has(id));
console.log(filtered);
Note that Array.prototype.includes
and Array.prototype.indexOf
operations are O(N)
, not O(1)
, so if you use them instead of a Set
, they may take significantly longer.
Not sure if the claim Set.has is O(1) is correct. Please provide an answer to this question if it is correct. stackoverflow.com/questions/55057200/…
– Charlie H
2 hours ago
Does it not depend on the size of the array? If the array is small, is it not better to through it than creating a new copy in memory then access it ?
– Grégory NEUT
51 secs ago
add a comment |
Turn the idsToRemove
into a Set
so that you can use Set.prototype.has
(an O(1)
operation), and .filter
the objList
just once, so that the overall complexity is O(n)
(and you only iterate over the possibly-huge objList
once):
var idsToRemove = ["3", "1"];
var objList = [{
id: "1",
name: "aaa"
},
{
id: "2",
name: "bbb"
},
{
id: "3",
name: "ccc"
}
];
const set = new Set(idsToRemove);
const filtered = objList.filter(({ id }) => !set.has(id));
console.log(filtered);
Note that Array.prototype.includes
and Array.prototype.indexOf
operations are O(N)
, not O(1)
, so if you use them instead of a Set
, they may take significantly longer.
Not sure if the claim Set.has is O(1) is correct. Please provide an answer to this question if it is correct. stackoverflow.com/questions/55057200/…
– Charlie H
2 hours ago
Does it not depend on the size of the array? If the array is small, is it not better to through it than creating a new copy in memory then access it ?
– Grégory NEUT
51 secs ago
add a comment |
Turn the idsToRemove
into a Set
so that you can use Set.prototype.has
(an O(1)
operation), and .filter
the objList
just once, so that the overall complexity is O(n)
(and you only iterate over the possibly-huge objList
once):
var idsToRemove = ["3", "1"];
var objList = [{
id: "1",
name: "aaa"
},
{
id: "2",
name: "bbb"
},
{
id: "3",
name: "ccc"
}
];
const set = new Set(idsToRemove);
const filtered = objList.filter(({ id }) => !set.has(id));
console.log(filtered);
Note that Array.prototype.includes
and Array.prototype.indexOf
operations are O(N)
, not O(1)
, so if you use them instead of a Set
, they may take significantly longer.
Turn the idsToRemove
into a Set
so that you can use Set.prototype.has
(an O(1)
operation), and .filter
the objList
just once, so that the overall complexity is O(n)
(and you only iterate over the possibly-huge objList
once):
var idsToRemove = ["3", "1"];
var objList = [{
id: "1",
name: "aaa"
},
{
id: "2",
name: "bbb"
},
{
id: "3",
name: "ccc"
}
];
const set = new Set(idsToRemove);
const filtered = objList.filter(({ id }) => !set.has(id));
console.log(filtered);
Note that Array.prototype.includes
and Array.prototype.indexOf
operations are O(N)
, not O(1)
, so if you use them instead of a Set
, they may take significantly longer.
var idsToRemove = ["3", "1"];
var objList = [{
id: "1",
name: "aaa"
},
{
id: "2",
name: "bbb"
},
{
id: "3",
name: "ccc"
}
];
const set = new Set(idsToRemove);
const filtered = objList.filter(({ id }) => !set.has(id));
console.log(filtered);
var idsToRemove = ["3", "1"];
var objList = [{
id: "1",
name: "aaa"
},
{
id: "2",
name: "bbb"
},
{
id: "3",
name: "ccc"
}
];
const set = new Set(idsToRemove);
const filtered = objList.filter(({ id }) => !set.has(id));
console.log(filtered);
edited 20 hours ago
answered 21 hours ago
CertainPerformanceCertainPerformance
91.6k165380
91.6k165380
Not sure if the claim Set.has is O(1) is correct. Please provide an answer to this question if it is correct. stackoverflow.com/questions/55057200/…
– Charlie H
2 hours ago
Does it not depend on the size of the array? If the array is small, is it not better to through it than creating a new copy in memory then access it ?
– Grégory NEUT
51 secs ago
add a comment |
Not sure if the claim Set.has is O(1) is correct. Please provide an answer to this question if it is correct. stackoverflow.com/questions/55057200/…
– Charlie H
2 hours ago
Does it not depend on the size of the array? If the array is small, is it not better to through it than creating a new copy in memory then access it ?
– Grégory NEUT
51 secs ago
Not sure if the claim Set.has is O(1) is correct. Please provide an answer to this question if it is correct. stackoverflow.com/questions/55057200/…
– Charlie H
2 hours ago
Not sure if the claim Set.has is O(1) is correct. Please provide an answer to this question if it is correct. stackoverflow.com/questions/55057200/…
– Charlie H
2 hours ago
Does it not depend on the size of the array? If the array is small, is it not better to through it than creating a new copy in memory then access it ?
– Grégory NEUT
51 secs ago
Does it not depend on the size of the array? If the array is small, is it not better to through it than creating a new copy in memory then access it ?
– Grégory NEUT
51 secs ago
add a comment |
You can use Array.includes
which check if the given string exists in the given array and combine it with an Array.filter
.
const idsToRemove = ['3', '1'];
const objList = [{
id: '1',
name: 'aaa',
},
{
id: '2',
name: 'bbb',
},
{
id: '3',
name: 'ccc',
},
];
const filteredObjList = objList.filter(x => !idsToRemove.includes(x.id));
console.log(filteredObjList);
add a comment |
You can use Array.includes
which check if the given string exists in the given array and combine it with an Array.filter
.
const idsToRemove = ['3', '1'];
const objList = [{
id: '1',
name: 'aaa',
},
{
id: '2',
name: 'bbb',
},
{
id: '3',
name: 'ccc',
},
];
const filteredObjList = objList.filter(x => !idsToRemove.includes(x.id));
console.log(filteredObjList);
add a comment |
You can use Array.includes
which check if the given string exists in the given array and combine it with an Array.filter
.
const idsToRemove = ['3', '1'];
const objList = [{
id: '1',
name: 'aaa',
},
{
id: '2',
name: 'bbb',
},
{
id: '3',
name: 'ccc',
},
];
const filteredObjList = objList.filter(x => !idsToRemove.includes(x.id));
console.log(filteredObjList);
You can use Array.includes
which check if the given string exists in the given array and combine it with an Array.filter
.
const idsToRemove = ['3', '1'];
const objList = [{
id: '1',
name: 'aaa',
},
{
id: '2',
name: 'bbb',
},
{
id: '3',
name: 'ccc',
},
];
const filteredObjList = objList.filter(x => !idsToRemove.includes(x.id));
console.log(filteredObjList);
const idsToRemove = ['3', '1'];
const objList = [{
id: '1',
name: 'aaa',
},
{
id: '2',
name: 'bbb',
},
{
id: '3',
name: 'ccc',
},
];
const filteredObjList = objList.filter(x => !idsToRemove.includes(x.id));
console.log(filteredObjList);
const idsToRemove = ['3', '1'];
const objList = [{
id: '1',
name: 'aaa',
},
{
id: '2',
name: 'bbb',
},
{
id: '3',
name: 'ccc',
},
];
const filteredObjList = objList.filter(x => !idsToRemove.includes(x.id));
console.log(filteredObjList);
answered 21 hours ago
Grégory NEUTGrégory NEUT
9,39421940
9,39421940
add a comment |
add a comment |
You don't need two nested iterators if you use a built-in lookup function
objList = objList.filter(o => idsToRemove.indexOf(o.id) < 0);
Documentation:
Array.prototype.indexOf()
Array.prototype.includes()
add a comment |
You don't need two nested iterators if you use a built-in lookup function
objList = objList.filter(o => idsToRemove.indexOf(o.id) < 0);
Documentation:
Array.prototype.indexOf()
Array.prototype.includes()
add a comment |
You don't need two nested iterators if you use a built-in lookup function
objList = objList.filter(o => idsToRemove.indexOf(o.id) < 0);
Documentation:
Array.prototype.indexOf()
Array.prototype.includes()
You don't need two nested iterators if you use a built-in lookup function
objList = objList.filter(o => idsToRemove.indexOf(o.id) < 0);
Documentation:
Array.prototype.indexOf()
Array.prototype.includes()
edited 20 hours ago
answered 21 hours ago
Charlie HCharlie H
9,51642653
9,51642653
add a comment |
add a comment |
Simply use Array.filter()
const idsToRemove = ['3', '1'];
const objList = [{
id: '1',
name: 'aaa',
},
{
id: '2',
name: 'bbb',
},
{
id: '3',
name: 'ccc',
}
];
const res = objList.filter(value => !idsToRemove.includes(value.id));
console.log("result",res);
add a comment |
Simply use Array.filter()
const idsToRemove = ['3', '1'];
const objList = [{
id: '1',
name: 'aaa',
},
{
id: '2',
name: 'bbb',
},
{
id: '3',
name: 'ccc',
}
];
const res = objList.filter(value => !idsToRemove.includes(value.id));
console.log("result",res);
add a comment |
Simply use Array.filter()
const idsToRemove = ['3', '1'];
const objList = [{
id: '1',
name: 'aaa',
},
{
id: '2',
name: 'bbb',
},
{
id: '3',
name: 'ccc',
}
];
const res = objList.filter(value => !idsToRemove.includes(value.id));
console.log("result",res);
Simply use Array.filter()
const idsToRemove = ['3', '1'];
const objList = [{
id: '1',
name: 'aaa',
},
{
id: '2',
name: 'bbb',
},
{
id: '3',
name: 'ccc',
}
];
const res = objList.filter(value => !idsToRemove.includes(value.id));
console.log("result",res);
const idsToRemove = ['3', '1'];
const objList = [{
id: '1',
name: 'aaa',
},
{
id: '2',
name: 'bbb',
},
{
id: '3',
name: 'ccc',
}
];
const res = objList.filter(value => !idsToRemove.includes(value.id));
console.log("result",res);
const idsToRemove = ['3', '1'];
const objList = [{
id: '1',
name: 'aaa',
},
{
id: '2',
name: 'bbb',
},
{
id: '3',
name: 'ccc',
}
];
const res = objList.filter(value => !idsToRemove.includes(value.id));
console.log("result",res);
edited 9 hours ago
dwirony
4,27231334
4,27231334
answered 20 hours ago
Khyati SharmaKhyati Sharma
1387
1387
add a comment |
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%2f55041973%2fremove-object-from-array-based-on-array-of-some-property-of-that-object%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