0

Consider I have the following variable:

var result = {
    "Items": [
      {
        "location": "New York",
        "visible": false,
        "destinations": [
          4,
          3,
          5
        ],
        "id": 3,
        "coordinates": {
          "lng": -74.17,
          "lat": 40.68
        }
      },{
        "location": "Madrid",
        "visible": false,
        "destinations": [
          0,
          4
        ],
        "id": 0,
        "coordinates": {
          "lng": -3.56,
          "lat": 40.49
        }
      },
      {
        "location": "Los Angeles",
        "visible": false,
        "destinations": [
          4,
          3,
          5
        ],
        "id": 5,
        "coordinates": {
          "lng": -118.4,
          "lat": 33.94
        }
      }
    ]
  };

If I want to reference to Los Angeles, I would do as below and it works great:

  console.log(result.Items[2]);

What if I don't know the order but I know its id (5). How can I reference it by its ID?

0

3 Answers 3

4

You can't reference it directly, but you can use some Javascript function on the array to find it, such as:

result.Items.find(x => x.id == 5);

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

Sign up to request clarification or add additional context in comments.

Comments

1

Filter your input like that

let newFilteredArray = result.filter(item => {
    return item.id === 5;
};
console.log(newFilteredArray[0];

Comments

0

You can do:

    var result = {
        "Items": [
            {
                "location": "New York",
                "visible": false,
                "destinations": [
                    4,
                    3,
                    5
                ],
                "id": 3,
                "coordinates": {
                    "lng": -74.17,
                    "lat": 40.68
                }
            }, {
                "location": "Madrid",
                "visible": false,
                "destinations": [
                    0,
                    4
                ],
                "id": 0,
                "coordinates": {
                    "lng": -3.56,
                    "lat": 40.49
                }
            },
            {
                "location": "Los Angeles",
                "visible": false,
                "destinations": [
                    4,
                    3,
                    5
                ],
                "id": 5,
                "coordinates": {
                    "lng": -118.4,
                    "lat": 33.94
                }
            }
        ]
    };
    
    
    var array = JSON.parse(JSON.stringify(result));
    
    for (i = 0; i <= array.Items.length - 1; i++) {
        if (array.Items[i].id == 5) {
            console.log(array.Items[i])
        }
    }

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.