I have the following JSON Array data from which I want to print Charlie. How to do it?
[ [ {
"ID": 1,
"Name" :"David"
},{
"ID": 2,
"Name" :"Antony"
}],[{
"ID": 1,
"Name" :"Bob"
},{
"ID": 2,
"Name" :"Charlie"
} ] ]
I have the following JSON Array data from which I want to print Charlie. How to do it?
[ [ {
"ID": 1,
"Name" :"David"
},{
"ID": 2,
"Name" :"Antony"
}],[{
"ID": 1,
"Name" :"Bob"
},{
"ID": 2,
"Name" :"Charlie"
} ] ]
you are using {PostData.map((group, index)=>{ return <p>{group.Name}</p>, since you are using map here PostData[0].Name will throw error inside map(), because you are iterating array with map().
Since you only need first item, you can simply use <p>PostData[0].Name</p>
var data = [{
"ID": 1,
"Name" :"David",
}, {
"ID": 2,
"Name" :"Antony",
}];
console.log(data[0].Name)
{PostData.map((group, index)=>{ return <p>{postData[0].Name}</p> } ??.map(), use like {PostData && <p>{PostData[0].Name}</p>}I tried all of the above and although the console.log(data[0].Name) would work to test the value, when it came to inserting the chosen value within the returned html code, I needed to use the code below. The reason is when we return the rows of data, only one row is valid and we need to use the "?" condition, as explained here https://stackoverflow.com/a/62824031/7496293 to exclude all the other cases and not have your code break:
{data[0]?.Name}