I have two arrays. First one is an array of indexes and second one is an array of objects. They look like this:
var nums = [0, 2];
var obj = [Object_1, Object_2, Object_3];
In this particular case I need to remove all "obj
" elements except obj[0]
and obj[2]
. So the result will look like this:
obj = [Object_2]
There are also may be cases when nums = [0, 1, 2]
and obj = [Object_1, Object_2, Object_3]
; In that case I dont need to remove any elements.
The "obj
" length is always greater than "nums" length.
So I started with finding only the elements that I need to save:
nums.forEach(function(key) {
obj.forEach(function(o, o_key) {
if (key === o_key) {
console.log(key, o);
// deleting remaining elements
}
});
});
The question: How can I remove elements that dont meets my condition? I dont need the new array, I want to modify the existing "obj" array. How can I achieve this functionality? Or should I use some another techniques?
nums = []
– Yerkovich