Reputation: 1515
I have an array of arrays like below.
array = [[false, 1, "label", "label value", null],[false, 2, "label1", "label1", null]]
I want to find the row matching to checkNum and return that row. checkNum is compared to the second index element. I don't want to put a for loop something like below,
checkNum = 1;
for (let i = 0; i < array.length; i++) {
if ((array[i][1]) === checkNum) {
}
}
Upvotes: 1
Views: 9882
Reputation: 386550
You could find the item with Array#find
.
var array = [[false, 1, "label", "label value", null], [false, 2, "label1", "label1", null]],
checkNum = 1,
result = array.find(a => a[1] === checkNum);
console.log(result);
Upvotes: 3
Reputation: 191946
Use Array.filter()
to get an array of items that match the criteria, or Array.find()
the get the 1st item that matches.
const array = [[false, 1, "label", "label value", null],[false, 2, "label1", "label1", null]]
const checkNum = 1
console.log(array.filter(({ 1: n }) => n === checkNum)) // array of items
console.log(array.find(({ 1: n }) => n === checkNum)) // 1st item found
Upvotes: 4