Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

285
Views
How to get the object value based on a certain condition in JavaScript?

I am playing with JS objects. And I came across a challenging situation for me:

basically I have a JS 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"
         },
         {
            "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.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Something like this?

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")}`);

about 4 years ago · Juan Pablo Isaza Report

0

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")

}

Heres the docs in using find. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

about 4 years ago · Juan Pablo Isaza Report

0

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"));

Lets do it in a more smart way. I hope It will work.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!