Estoy jugando con objetos JS. Y me encontré con una situación desafiante para mí:
básicamente tengo un objeto 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" } ] } }Tengo algo que puede obtener los valores de un objeto específico:
Object.keys(cinfo).forEach(function(ckey) { cinfo[ckey].service.forEach(function(skey){ console.log(skey.laborcost); }) }) Basado en el objeto anterior, la salida de la consola es: 500.00
Pero, quiero algo condicional cuando se trata de othercharges objeto de cargos. Necesito obtener la cantidad basada solo en la descripción:
Algo como esto:
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"; }) }¿Cómo hacerlo posible? Gracias.
¿Algo como esto?
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")}`);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") }Aquí están los documentos en el uso de find. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
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"));Hagámoslo de una manera más inteligente. Espero que funcione.