2

I want to log each occurrence of the letter an in this array, the expected output should be 3 a's, but there are none because it does dot read nested arrays does anyone know a solution.

let items = [["a","b"],["c","a"],["b","a"]];
items.forEach((v) => (v === "a" && console.log(v)));

2 Answers 2

2

You can flatten the array before looping, which will work for any level of nesting.

let items = [["a","b"],["c","a"],["b","a"]];
items.flat(Infinity).forEach((v) => (v === "a" && console.log(v)));

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

Comments

1

Flat array > Filter array > Foreach on filtered array

    let items = [["a","b"],["c","a"],["b","a"]];
    items.flat(Infinity)
         .filter((v) => (v === "a"))
         .forEach((v) => (console.log(v)));

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.