I have an array of arrays aa = [[value, value2], [value3, value4]] and I need to check if my new array a = [value2, value7] includes any value from aa I had tried this code and then i tried with adding .every to the aa but doesnt work. what should i do
a.some(r=> aa.includes(r))
a.some(r=> aa.every.includes(r))
asked Apr 3, 2020 at 23:06
user10877669498464651468548513
3053 silver badges11 bronze badges
1 Answer 1
Since you are using a nested array, you will want to use Array.prototype.flat on the aa array first. This will convert:
[[value, value2], [value3, value4]]
...to:
[value, value2, value3, value4]
See proof-of-concept below:
const aa = [[1, 2], [3, 4]];
const a = [2, 7];
// Flatten nested arrays
const aaFlat = aa.flat();
console.log(a.some(x => aaFlat.includes(x)));
answered Apr 3, 2020 at 23:10
Terry
66.7k16 gold badges107 silver badges127 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js
a.some(value => aa.some(inner => inner.includes(value))