skip to Main Content

I have this array :

data = {
  "lists": {
    "list-1": {
      "id": "list-1",
      "title": "list1",
      "cards": [
        {
          "id": "20",
          "title": "title"
        },
        {
          "id": "2",
          "title": "title"
        }
      ]
    },
    "list-2": {
      "id": "list-2",
      "title": "list2",
      "cards": [
        {
          "id": "4",
          "title": "title"
        },
        {
          "id": "3",
          "title": "title"
        }
      ]
    }
  }
}

I want to find the highest item with "id" as key. So here it would be "20"

I know how to do within an array but not from a multi dimensional array like here

const list = data.lists[listId]

3

Answers


  1. const entryWithHighestID = 
      Object.entries(data.lists)          // from all the entries of data.lists
        .map(([key, { cards }]) => cards) // extract the cards property
        .flat()                           // flat to a single array
        .sort((a, b) => b.id - a.id)[0];  // sort by id descending, and get the first entry
    
    
    Login or Signup to reply.
  2. Reduce the lists to a min value of card ids:

    const max = Object.keys(data.lists).reduce((r, key) => data.lists[key].cards.forEach(card => +card.id > r.id && (r = card)) || r, {id: -1});
    console.log(max);
    <script>
    const data={lists:{"list-1":{id:"list-1",title:"list1",cards:[{id:"20",title:"title"},{id:"2",title:"title"}]},"list-2":{id:"list-2",title:"list2",cards:[{id:"4",title:"title"},{id:"3",title:"title"}]}}};
    </script>

    If you want the maximum speed, just loop:

    let max = {id: -1};
    
    for(const key in data.lists){
      const cards = data.lists[key].cards;
      for(let i = 0; i < cards.length; i++){
        const card = cards[i];
        if(+card.id > max.id) max = card;
      }
    }
    console.log(max);
    <script>
    const data={lists:{"list-1":{id:"list-1",title:"list1",cards:[{id:"20",title:"title"},{id:"2",title:"title"}]},"list-2":{id:"list-2",title:"list2",cards:[{id:"4",title:"title"},{id:"3",title:"title"}]}}};
    </script>
    ` Chrome/117
    ----------------------------------------------------------------------------
    Alexander fast looping           1.0x  |  x10000000  334  343  344  345  358
    pilchard's tweak to one-liner    1.2x  |  x10000000  397  400  405  407  410
    Alexander one-liner              1.3x  |  x10000000  424  427  429  429  432
    moonwave99                      15.3x  |   x1000000  512  512  513  519  521
    ----------------------------------------------------------------------------
    https://github.com/silentmantra/benchmark `
    
    const data={lists:{"list-1":{id:"list-1",title:"list1",cards:[{id:"20",title:"title"},{id:"2",title:"title"}]},"list-2":{id:"list-2",title:"list2",cards:[{id:"4",title:"title"},{id:"3",title:"title"}]}}};
    
    // @benchmark moonwave99
    Object.entries(data.lists)         // from all the entries of data.lists
        .map(([key, { cards }]) => cards) // extract the cards property
        .flat()                           // flat to a single array
        .sort((a, b) => b.id - a.id)[0];
    
    // @benchmark pilchard's tweak to one-liner
    Object.keys(data.lists).reduce((r, key) => data.lists[key].cards.reduce((c, card) => +card.id > c.id ? card : c, r), {id: -1});
    
    // @benchmark Alexander one-liner
    Object.keys(data.lists).reduce((r, key) => data.lists[key].cards.forEach(card => +card.id > r.id && (r = card)) || r, {id: -1});
    
    // @benchmark Alexander fast looping
    let max = {id: -1};
    
    for(const key in data.lists){
      const cards = data.lists[key].cards;
      for(let i = 0; i < cards.length; i++){
        const card = cards[i];
        if(+card.id > max.id) max = card;
      }
    }
    max;
    
    /*@end*/eval(atob('e2xldCBlPWRvY3VtZW50LmJvZHkucXVlcnlTZWxlY3Rvcigic2NyaXB0Iik7aWYoIWUubWF0Y2hlcygiW2JlbmNobWFya10iKSl7bGV0IHQ9ZG9jdW1lbnQuY3JlYXRlRWxlbWVudCgic2NyaXB0Iik7dC5zcmM9Imh0dHBzOi8vY2RuLmpzZGVsaXZyLm5ldC9naC9zaWxlbnRtYW50cmEvYmVuY2htYXJrL2xvYWRlci5qcyIsdC5kZWZlcj0hMCxkb2N1bWVudC5oZWFkLmFwcGVuZENoaWxkKHQpfX0='));
    Login or Signup to reply.
  3. You can find the highest item with the "id" key from the given multidimensional array by iterating through the nested structures and keeping track of the highest value found. Here’s a JavaScript function that accomplishes this:

    Most Efficient method: 120OP/s

    const data = {
      "lists": {
        "list-1": {
          "id": "list-1",
          "title": "list1",
          "cards": [
            {
              "id": "20",
              "title": "title"
            },
            {
              "id": "2",
              "title": "title"
            }
          ]
        },
        "list-2": {
          "id": "list-2",
          "title": "list2",
          "cards": [
            {
              "id": "4",
              "title": "title"
            },
            {
              "id": "3",
              "title": "title"
            }
          ]
        }
      }
    };
    function findHighestId(data) {
      let highestId = -1;
    
      for (const listKey in data.lists) {
        const list = data.lists[listKey];
    
        for (const card of list.cards) {
          const cardId = parseInt(card.id, 10);
    
          if (!isNaN(cardId) && cardId > highestId) {
            highestId = cardId;
          }
        }
      }
    
      return highestId;
    }
    
    const highestId = findHighestId(data);
    console.log("Highest ID:", highestId); // Output: Highest ID: 20

    Another way to write it: 86K OP/s

    const data = {
      "lists": {
        "list-1": {
          "id": "list-1",
          "title": "list1",
          "cards": [
            {
              "id": "20",
              "title": "title"
            },
            {
              "id": "2",
              "title": "title"
            }
          ]
        },
        "list-2": {
          "id": "list-2",
          "title": "list2",
          "cards": [
            {
              "id": "4",
              "title": "title"
            },
            {
              "id": "3",
              "title": "title"
            }
          ]
        }
      }
    };
    
    function findHighestId(data) {
      return Object.values(data.lists)
        .flatMap(list => list.cards)
        .map(card => parseInt(card.id))
        .filter(id => !isNaN(id))
        .reduce((highestId, currentId) => Math.max(highestId, currentId), -1);
    }
    
    const highestId = findHighestId(data);
    console.log("Highest ID:", highestId); // Output: Highest ID: 20

    One last attempt… this can be done many ways: 86K OP/s

    const data = {
      "lists": {
        "list-1": {
          "id": "list-1",
          "title": "list1",
          "cards": [
            {
              "id": "20",
              "title": "title"
            },
            {
              "id": "2",
              "title": "title"
            }
          ]
        },
        "list-2": {
          "id": "list-2",
          "title": "list2",
          "cards": [
            {
              "id": "4",
              "title": "title"
            },
            {
              "id": "3",
              "title": "title"
            }
          ]
        }
      }
    };
    
    
    const highestId = Object.values(data.lists)
      .flatMap(list => list.cards)
      .reduce((highest, card) => {
        const cardId = parseInt(card.id);
        return cardId > highest ? cardId : highest;
      }, -1);
    
    console.log("Highest ID:", highestId);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search