I have a two-dimensional array like this:
all = [
[bla, bla, ID1, bla, bla],
[bla, bla, ID2, bla, bla],
[bla, bla, ID3, bla, bla],
[bla, bla, ID4, bla, bla],
[bla, bla, ID5, bla, bla],
[bla, bla, ID6, bla, bla]
]
Next, I have a one-dimensional array like this:
selected = [ID1, ID2, ID6]
I need to remove all entries from my array all where the ID value does not match any ID in my array selected. From the example above I would then get a new array like this:
reduced = [
[bla, bla, ID1, bla, bla],
[bla, bla, ID2, bla, bla],
[bla, bla, ID6, bla, bla]
]
How can I get an array like this?
Thanks for helping.
You can one line this using
Array.prototype.filterIt simply means, if
all[i][2](the value from the ID col) is in the selected array, keep this entry. It builds a new Array soallandselectedwon’t be affected by this process.It may be slightly faster to use a loop to do the same test, as it will mean less function overhead