Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I get a value from a json file that is identified by a number

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading