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)));
CodePudding user response:
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)));
CodePudding user response:
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)));