Here is the JSON object where i wanted to display on of the values
"asset_risks": [
{
"Medium": 2
},
{
"High": 11
},
{
"Low": 3
}
],
One solution is to combine all object inside the asset_risks array into a single object and then get its attribute like this:
const data = {
"asset_risks": [
{
"Medium": 2
},
{
"High": 11
},
{
"Low": 3
}
]
};
const combined = Object.fromEntries(data.asset_risks.map(Object.entries).flat());
console.log(combined);
console.log(combined.Low);
Couple of ways to do that.
One way is you can make use of javascript filter. You can filter out the array based on Low and take the value from the filtered array.
Another way is to make use of find. Please check out the code below for reference.
var obj = {
"asset_risks": [
{
"Medium": 2
},
{
"High": 11
},
{
"Low": 3
}
]
}
console.log(obj.asset_risks.find(obj => obj.Low).Low);
console.log(obj.asset_risks.filter(obj => { return obj.Low })[0].Low);