0

I have array

[
["BS-BLACK-2", ..., "0"]
["BS-BLACK-3", ..., "0"],
["BS-BLACK-4", ..., "0"],
["BS-BLACK-5", ..., "0"]
]

And another array

["BS-BLACK-2","BS-BLACK-3"]

How to exclude all elements in 1st array if values found in second array. to have:

[["BS-BLACK-4", ..., "0"],["BS-BLACK-5", ..., "0"]]

I use below code but it works only with not nested arrays

newArray= oldArray.filter(function (el) {
             return !toExcludeAray.includes(el);
}
1
  • 1
    Are the values to exclude always at index 0 in the nested arrays? Commented Jun 30, 2020 at 12:45

2 Answers 2

1

You can use includes() inside a filter() call on your input data to exclude the elements based on the second array. [firstValue] corresponds to destructuring the first value in the nested array element.

const input = [
    ["BS-BLACK-2", "0"],
    ["BS-BLACK-3", "0"],
    ["BS-BLACK-4", "0"],
    ["BS-BLACK-5", "0"]
];
const toExclude = ["BS-BLACK-2","BS-BLACK-3"];
const filtered = input.filter(([firstValue]) => (!toExclude.includes(firstValue)));
console.log(filtered);

Sign up to request clarification or add additional context in comments.

Comments

0

You can use el[0] if BS-BLACK-2, BS-BLACK-3.. always at 0th index.

let oldArray = [
["BS-BLACK-2", "0"],
["BS-BLACK-3", "0"],
["BS-BLACK-4", "0"],
["BS-BLACK-5", "0"]
];


let toExcludeAray = ["BS-BLACK-2","BS-BLACK-3"];

let newArray = oldArray.filter(function (el) {
   return !toExcludeAray.includes(el[0]);
});
console.log(newArray);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.