I have a question of JS arrays.
Example:
var fullArr = [1,2,3,4];
var partArr = [2,3];
var newArr = [];
We have a main array fullArr
and a partial array partarr
. I want to create a function/filter, which is looking for existing items in fullArr
and not in partArr
.
In this example above newArr
must be equal to [1,4].
I've tried doing something like this, but it's not working properly.
for (var k in fullArray) { // [1,2,3,4]
for (var j in selectedArray) { // [1,4]
if (fullArray[k] == selectedArray[j]) {
newArray.splice(selectedArray[j] - 1, 1); // must be [2,3]
break;
}
}
}
What is a good way of making this? Thanks.
but it's not working properly
how's it not working? What result does it give you?for...in
with an array because the order isn't guaranteed. Use a regularfor
loop.Array.filter
is the easier solution, but you might also want to look at the documentation for Array.splice because you aren't using it right. But since you are (presumably) trying to add an item tonewArr
, it would be easier to do withpush
.