Reputation: 21
I am new in JSON Javascript. I have an object and I want to extract only the text value.
This is my code, but when I'm running it the result is undefined
:
var obj = [{
"start_location":
{
"lat":47.951042801290065,
"lng":-116.70764186944393
},
"end_location":
{
"lat":47.94871454878046,
"lng":-116.70839074239734
},
"length":
{
"text":"265.13 m",
"value":265.126074613001
}
},
{
"start_location":
{
"lat":47.94871454878046,
"lng":-116.70839074239734
},
"end_location":
{
"lat":47.949763712586105,
"lng":-116.70401337728504
},
"length":
{
"text":"346.65 m",
"value":346.6461031139708
}
},
{
"start_location":
{
"lat":47.949763712586105,
"lng":-116.70401337728504
},
"end_location":
{
"lat":47.951042801290065,
"lng":-116.70764186944393
},
"length":
{
"text":"305.72 m",
"value":305.7189215338448
}
}
];
var myJSON = JSON.stringify(obj);
document.getElementById("demo").innerHTML = myJSON[['length'],['text']];
Upvotes: 0
Views: 588
Reputation: 13079
Why are you stringify'ing it. Easier to get from JS object than JSON formatted string.
var obj = [
{
"start_location": { "lat": 47.951042801290065, "lng": -116.70764186944393 },
"end_location": { "lat": 47.94871454878046, "lng": -116.70839074239734 },
"length": { "text": "265.13m", "value": 265.126074613001 }
},
{
"start_location": { "lat": 47.94871454878046, "lng": -116.70839074239734 },
"end_location": { "lat": 47.949763712586105, "lng": -116.70401337728504 },
"length": { "text": "346.65m", "value": 346.6461031139708 }
},
{
"start_location": { "lat": 47.949763712586105, "lng": -116.70401337728504 },
"end_location": { "lat": 47.951042801290065, "lng": -116.70764186944393 },
"length": { "text": "305.72m", "value": 305.7189215338448 }
}
];
let lengths = obj.map(item => item.length.text);
console.log(lengths);
Upvotes: 2