skip to Main Content

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?

3

Answers


  1. You can use find() or filter() depending on how many results you expect.

    In case there are no results, find() returns undefined and filter() returns an empty list, so it’s easy to check for results.

    const fruits = [
    ["bananas", 123],
    ["strawberries", 456],
    ["kiwi", 789],
    ["mango", 098],
    ["apple", 543],
    ];
    
    const input = "apple";
    
    // multiple results
    const results = fruits.filter(x => x[0] === input);
    if(results.length !== 0){ // check if we have at least one result
      console.log(results.map(x => x[1])); // instead of whole array, only log the number
    }
    // single result
    const result = fruits.find(x => x[0] === input);
    if(result !== undefined){ // check if we have a result
      console.log(result[1]); 
    }

    Another way, better suited if you want to do multiple lookups is to convert the 2D array to a JS object (using Object.fromEntries()) or Map which can do lookups more efficiently in constant time O(1) as opposed to linear time O(n).

    For this approach to work the keys (i. e. the fruit names in your example) need to be unique, so this only works for a single result.

    const fruits = [
    ["bananas", 123],
    ["strawberries", 456],
    ["kiwi", 789],
    ["mango", 098],
    ["apple", 543],
    ];
    
    const input = "apple";
    
    // Approach with JS object
    // convert to JS object
    const fruitsObj = Object.fromEntries(fruits);
    // lookup in the object using the key
    console.log(fruitsObj[input] ?? "Not found");
    
    
    // Approach with Map
    const fruitsMap = new Map(fruits);
    // lookup in the Map using the key
    console.log(fruitsMap.get(input) ?? "Not found");
    Login or Signup to reply.
  2. 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
    Login or Signup to reply.
  3. It would be better to convert your array from Google Sheets into an object like this:

    const fruits = [
      ['bananas', 123],
      ['strawberries', 456],
      ['kiwi', 789],
      ['mango', 098],
      ['apple', 543],
    ]
    
    const fruitsObject = fruits.reduce(
      (accumulator, currentValue) => ({
        ...accumulator,
        [currentValue[0]]: currentValue[1],
      }),
      {}
    )
    
    console.log(fruitsObject)
    console.log(fruitsObject['bananas'])
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search