I am playing with JSON objects. And I came across a challenging situation for me:
basically I have a JSON object:
let cinfo = {
"costing_T063623477Z":{
"service":[
{
"objid":"T063637283Z",
"serviceid":"SRV2100003",
"servicename":"FABRICATION OF SPRINKLER & HOSE",
"estimatedprice":"10000.00",
"description":"sdfg",
"laborcost":"500.00"
}
],
"othercharges":[
{
"objid":"T063911531Z",
"description":"Other Expenses",
"amount":"345.00",
"remarks":"345"
},
{
"objid":"T063906963Z",
"description":"Sales Expenses",
"amount":"345.00",
"remarks":"345"
},
{
"objid":"T063730836Z",
"description":"Delivery Expenses",
"amount":"345.00",
"remarks":"345"
}
]
}
}
I have something that can get the values of a specific object:
Object.keys(cinfo).forEach(function(ckey) {
cinfo[ckey].service.forEach(function(skey){
console.log(skey.laborcost);
})
})
Based on the object above, the console output is: 500.00
But, I want something conditional when it comes to othercharges
object.
I need to get the amount based only on the description:
Something like this:
Object.keys(cinfo).forEach(function(ckey) {
cinfo[ckey].othercharges.forEach(function(skey){
console.log(skey.amount) //-> where "description" = "Other Expenses";
console.log(skey.amount) //-> where "description" = "Sales Expenses";
console.log(skey.amount) //-> where "description" = "Delivery Expenses";
})
}
How to make it possible? Thanks.