Skip to content
Advertisement

How do i search by ID in json file? [closed]

so let’s say i have this JSON file:

{
  "1": {
    "name": "some random name 1",
    "nickname": "some random nickname 1"
  },
  "2": {
    "name": "some random name 2",
    "nickname": "some random nickname 2"
  }
}

And i need function that searches by ID,("1": / "2": is ID basically) and saves it in variable. (Using Javascript). I just don’t know how to do it.

Advertisement

Answer

Why not just standard bracket notation for this…?

var object = {
  "1": {
    "name": "some random name 1",
    "nickname": "some random nickname 1"
  },
  "2": {
    "name": "some random name 2",
    "nickname": "some random nickname 2"
  }
}

console.log(object["1"]);
console.log(object["2"]);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement