Warm tip: This article is reproduced from stackoverflow.com, please click
arrays ecmascript-6 javascript

Search nested array in lodash/es6

发布于 2020-03-27 10:23:03

Looking to search a value in an array of array and returning index. Most of the answers are array of objects. So I am looking to search for eg 22 and get 2 as the index where the value was found

Here is the code pen https://codesandbox.io/s/lodash-playground-array-pzzhe

const arr = [["a","b"],["f","r"],["r",22,"t"]];
console.log("arr", arr);
Questioner
June
Viewed
98
Nina Scholz 2019-07-03 22:33

You could take plain Javascript with Array#findIndex with Array#includes.

var array = [["a", "b"], ["f", "r"], ["r", 22, "t"]],
    value = 22,
    index  = array.findIndex(a => a.includes(value));
    
console.log(index);