【发布时间】:2021-12-29 06:54:50
【问题描述】:
我在玩 JS 对象。我遇到了一个对我来说具有挑战性的情况:
基本上我有一个 JS 对象:
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"
},
{
"objid":"T063730836Z",
"description":"Delivery Expenses",
"amount":"345.00",
"remarks":"345"
}
]
}
}
我有东西可以获取特定对象的值:
Object.keys(cinfo).forEach(function(ckey) {
cinfo[ckey].service.forEach(function(skey){
console.log(skey.laborcost);
})
})
根据上面的对象,控制台输出为:500.00
但是,当涉及到othercharges 对象时,我想要一些有条件的东西。
我只需要根据描述获取金额:
类似这样的:
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";
})
}
如何使它成为可能?谢谢。
【问题讨论】:
-
你应该停止玩JSON objects,开始玩JS对象。
标签: javascript arrays