javascript - How to find single matching value in two arrays in js -
i have 2 arrays.
var = ["a","b","c","d"]; var b = ["c","d","f","k"];
these 2 arrays.now need check single matching values in 2 arrays.it should break after finding single match value.
my expect result c , d matches.
you can use array#find iterate 1 of arrays, , array#indexof or array#includes find if item exists in 2nd array:
var = ["a", "b", "c", "d"]; var b = ["c", "d", "f", "k"]; function findmatch(arr1, arr2) { return arr1.find(function(item) { return arr2.indexof(item) === -1; }); } var result = findmatch(a, b); console.log(result);
and fancy version arrow functions , consts:
const = ["a","b","c","d"]; const b = ["c","d","f","k"]; const findmatch = (arr1, arr2) => arr1.find((item) => arr2.includes(item)); const result = findmatch(a, b); console.log(result);
Comments
Post a Comment