Skip to content
Advertisement

How to simply find matching values in [[], [], []] arrays in javascript

new to javascript. i have these two arrays

var array1 = [['1'],['2']];
var array2 = [['2'],['3'],['4']];

how can i find the matching values?

tried below but returns empty array probably because it’s for normal array structure [”, ”, ”]

var matchingValue = array1.filter(value => array2.includes(value));
Logger.log(matchingValue);

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 :-

var array1 = [['1'],['2']];
var array2 = [['2'],['3'],['4']];

var matchingValue = array1.flat().filter((value) => array2.flat().includes(value) )
console.log(matchingValue);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement