new to javascript. i have these two arrays
JavaScript
x
3
1
var array1 = [['1'],['2']];
2
var array2 = [['2'],['3'],['4']];
3
how can i find the matching values?
tried below but returns empty array probably because it’s for normal array structure [”, ”, ”]
JavaScript
1
3
1
var matchingValue = array1.filter(value => array2.includes(value));
2
Logger.log(matchingValue);
3
Matching value should be [‘2’]
Advertisement
Answer
You can simply use .flat()
to flatten the arrays so you only deal with the values like so :-
JavaScript
1
5
1
var array1 = [['1'],['2']];
2
var array2 = [['2'],['3'],['4']];
3
4
var matchingValue = array1.flat().filter((value) => array2.flat().includes(value) )
5
console.log(matchingValue);