I have been working on a javascript application and I am attempting to get the value of "id" using a different key/value pair. My JSON:
{
"data": [
{
"value": "Hermitage Park",
"type": "Park",
"id": 2
}
]
}
My Javascript:
function GetID(value) {
var output = 0
fetch("http://localhost:8088/data").then(storage =>
storage.json()).then(items => {
for (i = 0; i < items.length; i++) {
if (items[i]["value"] == value) {
output = i
break
}
}
})
return output
}
When calling the function, I specified "Hermitage Park" as the value, hoping to get back the value of the "id" property, which is 2. It seems right to me, but it always returns 0 when it should be 2. I also tried items[value]["id"]
and items[value].id
, but nothing is working so far.