Remove object from array based on array of some property of that object
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
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 Mar 7 at 12:52
Uwe Keim
27.7k32135216
27.7k32135216
asked Mar 7 at 10:48
DaliborDalibor
520318
520318
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
Mar 8 at 5:55
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
Mar 8 at 7:58
1
You're right, if theidsToRemove
has a very small number of elements, using a Set instead doesn't provide much of a benefit.
– CertainPerformance
Mar 8 at 8:31
Can you explain me the difference between your syntax and this syntax: const filtered = objList.filter(o => !set.has(o.id));
– Dalibor
Mar 9 at 11:42
1
@Dalibor My version uses destructuring in the argument list, whereas that version doesn't. If a function doesn't need the whole object, but only one (or some) of its properties, I like to extract that property immediately rather than having to hold on to the whole object - but both options work just fine.
– CertainPerformance
Mar 9 at 11:48
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
Mar 8 at 5:55
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
Mar 8 at 7:58
1
You're right, if theidsToRemove
has a very small number of elements, using a Set instead doesn't provide much of a benefit.
– CertainPerformance
Mar 8 at 8:31
Can you explain me the difference between your syntax and this syntax: const filtered = objList.filter(o => !set.has(o.id));
– Dalibor
Mar 9 at 11:42
1
@Dalibor My version uses destructuring in the argument list, whereas that version doesn't. If a function doesn't need the whole object, but only one (or some) of its properties, I like to extract that property immediately rather than having to hold on to the whole object - but both options work just fine.
– CertainPerformance
Mar 9 at 11:48
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
Mar 8 at 5:55
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
Mar 8 at 7:58
1
You're right, if theidsToRemove
has a very small number of elements, using a Set instead doesn't provide much of a benefit.
– CertainPerformance
Mar 8 at 8:31
Can you explain me the difference between your syntax and this syntax: const filtered = objList.filter(o => !set.has(o.id));
– Dalibor
Mar 9 at 11:42
1
@Dalibor My version uses destructuring in the argument list, whereas that version doesn't. If a function doesn't need the whole object, but only one (or some) of its properties, I like to extract that property immediately rather than having to hold on to the whole object - but both options work just fine.
– CertainPerformance
Mar 9 at 11:48
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 Mar 7 at 11:03
answered Mar 7 at 10:51
CertainPerformanceCertainPerformance
98.7k166089
98.7k166089
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
Mar 8 at 5:55
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
Mar 8 at 7:58
1
You're right, if theidsToRemove
has a very small number of elements, using a Set instead doesn't provide much of a benefit.
– CertainPerformance
Mar 8 at 8:31
Can you explain me the difference between your syntax and this syntax: const filtered = objList.filter(o => !set.has(o.id));
– Dalibor
Mar 9 at 11:42
1
@Dalibor My version uses destructuring in the argument list, whereas that version doesn't. If a function doesn't need the whole object, but only one (or some) of its properties, I like to extract that property immediately rather than having to hold on to the whole object - but both options work just fine.
– CertainPerformance
Mar 9 at 11:48
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
Mar 8 at 5:55
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
Mar 8 at 7:58
1
You're right, if theidsToRemove
has a very small number of elements, using a Set instead doesn't provide much of a benefit.
– CertainPerformance
Mar 8 at 8:31
Can you explain me the difference between your syntax and this syntax: const filtered = objList.filter(o => !set.has(o.id));
– Dalibor
Mar 9 at 11:42
1
@Dalibor My version uses destructuring in the argument list, whereas that version doesn't. If a function doesn't need the whole object, but only one (or some) of its properties, I like to extract that property immediately rather than having to hold on to the whole object - but both options work just fine.
– CertainPerformance
Mar 9 at 11:48
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
Mar 8 at 5:55
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
Mar 8 at 5:55
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
Mar 8 at 7:58
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
Mar 8 at 7:58
1
1
You're right, if the
idsToRemove
has a very small number of elements, using a Set instead doesn't provide much of a benefit.– CertainPerformance
Mar 8 at 8:31
You're right, if the
idsToRemove
has a very small number of elements, using a Set instead doesn't provide much of a benefit.– CertainPerformance
Mar 8 at 8:31
Can you explain me the difference between your syntax and this syntax: const filtered = objList.filter(o => !set.has(o.id));
– Dalibor
Mar 9 at 11:42
Can you explain me the difference between your syntax and this syntax: const filtered = objList.filter(o => !set.has(o.id));
– Dalibor
Mar 9 at 11:42
1
1
@Dalibor My version uses destructuring in the argument list, whereas that version doesn't. If a function doesn't need the whole object, but only one (or some) of its properties, I like to extract that property immediately rather than having to hold on to the whole object - but both options work just fine.
– CertainPerformance
Mar 9 at 11:48
@Dalibor My version uses destructuring in the argument list, whereas that version doesn't. If a function doesn't need the whole object, but only one (or some) of its properties, I like to extract that property immediately rather than having to hold on to the whole object - but both options work just fine.
– CertainPerformance
Mar 9 at 11:48
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 Mar 7 at 10:55
Grégory NEUTGrégory NEUT
9,73721941
9,73721941
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 Mar 7 at 11:08
answered Mar 7 at 10:54
Charlie HCharlie H
9,77042953
9,77042953
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 Mar 7 at 22:56
dwirony
4,65731434
4,65731434
answered Mar 7 at 10:59
Khyati SharmaKhyati Sharma
837
837
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