-2

So imagine this Theres a json file built like this

{
    "data": [
    
        {
            "id": "1",
            "name": "name1"
        },
        {
            "id": "2",
            "name": "name2"
        }
    ]
}

How would I go about grabbing the related value "name" from the json file if I knew the ID to it.

I know this might seem like a dumb question, but I'm new to JS and I hope to learn from this x.x

Jay
  • 23
  • 1
  • 6

1 Answers1

0

Use find

const data = {
    "data": [
        {
            "id": "1",
            "name": "name1"
        },
        {
            "id": "2",
            "name": "name2"
        }
    ]
}

const id = '1'

const name = data.data.find(obj => obj.id === id).name

console.log(name)
Konrad
  • 21,590
  • 4
  • 28
  • 64