-3

Hello I have the following JSON result. Its a small snippet from a large JSON file. Theres hundreds of entries.

var jsonResult = [
    {
        "id": "1",
        "name": "one",
    },
    {
        "id": "2",
        "name": "two",
    },
    {
        "id": "3",
        "name": "three",
    }
]

I would like a way to loop through this data using javascript to create a new array stored in a variable that pulls just the ids. So the new array would look like this:

data = [1, 2, 3];

If theres 1000 entries, then the array would go from 1-1000.

Thanks!

cup_of
  • 6,397
  • 9
  • 47
  • 94

1 Answers1

1

Very simple just use array.map.

var jsonResult = [
  {
      "id": "1",
      "name": "one",
  },
  {
      "id": "2",
      "name": "two",
  },
  {
      "id": "3",
      "name": "three",
  }
]

let idArray = jsonResult.map(element => element["id"]);
console.log(idArray)
Firas Omrane
  • 894
  • 1
  • 14
  • 21