This should be easy but my brain is fried.
I have a variable, and I want to search a 2D array that corresponds to matching value:
const fruits = [
["bananas", 123],
["strawberries", 456],
["kiwi", 789],
["mango", 098],
["apple", 543],
];
const input = "apple";
// result should be 543
What would be the shortest way to do so?
>Solution :
One solution would be to use the find() method to search for the matching value in your array.
For example:
const fruits = [
["bananas", 123],
["strawberries", 456],
["kiwi", 789],
["mango", 098],
["apple", 543],
];
const input = "apple";
const result = fruits.find(fruit => fruit[0] === input)?.[1];
console.log(result); // 543