If I had this schema:
[{
"Id": 2,
"Prizes": [{
"Id": 5,
"Status": 0,
"ClaimCode": "PN0016CXC1WPM64P",
"User": {
"FirstName": "John",
"SecondName": "Doe",
},
"DrawId": 2,
"PrizeId": 22,
"Prize": null
}]
}]
How could I get the first name or any value under object User that is in array prizes, that is in the main JSON?
I have tried this:
const response = JSON.parse(this.responseText);
response.Prizes[0].User.forEach(function(a) {
output += `<li>${a.FirstName}</li>`;
});
But getting the error undefined, I believe it can be extracted but only need right syntax. Or this can't be done with for loop?
responseis an Array, not a plain Object.forEachshould be used on an Array.for(let key in response[0].Prizes[0].User), but I don't think you neeed a loop for this.