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
>Solution :
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)