我在玩 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";
})
}
如何使它成為可能?謝謝。
uj5u.com熱心網友回復:
像這樣的東西?
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":"100.00",
"remarks":"345"
}
]
}
}
const getServiceCharge = (info) => {
const key = Object.keys(info)[0]; // Get first key in object
return info[key].service[0].laborcost;
}
const getOtherCharge = (info, desc) => {
const key = Object.keys(info)[0]; // Get first key in object
const otherCharges = info[key].othercharges;
const sumAmount = otherCharges
.filter(item => item.description === desc) // Get only items we are looking for
.reduce((sum, item) => sum Number(item.amount), 0); // Sum amount
return sumAmount;
}
console.log(`Service Labor charge: ${getServiceCharge(cinfo)}`);
console.log(`Service Other Expenses charge: ${getOtherCharge(cinfo, "Other Expenses")}`);
console.log(`Service Sales Expenses charge: ${getOtherCharge(cinfo, "Sales Expenses")}`);
console.log(`Service Delivery Expenses charge: ${getOtherCharge(cinfo, "Delivery Expenses")}`);
uj5u.com熱心網友回復:
Object.keys(cinfo).forEach(function(ckey) {
// Get only 1 other charges
// Return object
const deliveryExpenses = cinfo[key].othercharges.find(item => item.description == "Delivery Expenses")
// Get all other charges base on the comparison
// Returns array
const deliveryExpenses = cinfo[key].othercharges.filter(item => item.description == "Delivery Expenses")
}
下面是使用 find 的檔案。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
uj5u.com熱心網友回復:
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"
}
]
}
};
function getCharge(serviceCharge = null, otherCharges = null) {
if (serviceCharge) {
return cinfo.costing_T063623477Z.service[0].laborcost;
}
return cinfo.costing_T063623477Z.othercharges.filter(
(t) => t.description === otherCharges
).map(t=>t.amount);
}
console.log(getCharge(true));
console.log(getCharge(null, "Other Expenses"));
console.log(getCharge(null, "Sales Expenses"));
console.log(getCharge(null, "Delivery Expenses"));
讓我們以更聰明的方式來做。我希望它會起作用。
uj5u.com熱心網友回復:
這是一個答案,僅使用一個回圈(通過.reduce),并通過在正確的位置分配一些變數來設定每件事。
它適用于多個services或如果多個descriptions共享相同的值。
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":"100.00",
"remarks":"345"
}
]
}
}
function getCost(obj, othercharges) {
let key = Object.keys(obj)[0],
subKey = (othercharges) ? 'othercharges' : 'service',
property = (othercharges) ? 'amount' : 'laborcost',
childObj = obj[key][subKey];
return childObj.reduce((totalSum, item) => {
let itemSum = 0;
if (!othercharges || item.description == othercharges) {
itemSum = item[property];
}
return totalSum Number(itemSum);
}, 0);
}
console.log( getCost(cinfo) ); // returns 'laborcost'
console.log( getCost(cinfo, 'Delivery Expenses') );
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/397323.html
標籤:javascript 数组
