Iván
Iván

Reputation: 441

JavaScript, access json property

I have a .json file like this:

import single from 'file.json'


{
"id": 4,
"code": 4508099576,
"important": [
            {
            "id": 4,
            "name": "services"
            }
        ]

}

I want to access "services" for render it. But the following syntax doesn´t work:

<span>Plans: {single.important.name}</span>

Upvotes: 0

Views: 1067

Answers (2)

Shubham Khatri
Shubham Khatri

Reputation: 281626

important is an array and you need to access its first object. Your would write

<span>Plans: {single.important[0].name}</span>

In case if you want to render all the plans within the important array, you would make use of map like

<div>{single.important.map((obj) => {
   return  <span key={obj.id}>Plans: {obj.name}</span>
})</div>

Upvotes: 1

TheBeardedOne
TheBeardedOne

Reputation: 292

Important is an array. You need to access it with an index ex.

single.important[0].name

Upvotes: 0

Related Questions