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

156
Views
Finding highest count by sum up of an attribute in an Json array in javascript

I'm looking for clean and efficient way to get highest value count by sum up of all attributes in following json array.

[{ "id":1, "material":2, "noMaterial":3, "negative":1 }, { "id":2, "material":4, "noMaterial":3, "negative":3}, { "id":3, "material":0, "noMaterial":1, "negative":1}]

Expected Output:

{ "noMaterial": 7 }

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

0

This is not perfect way but may be it can be help to you.

var data = [{ 
    "id": 1, 
    "material": 2, 
    "noMaterial": 3, 
    "negative": 1
}, { 
    "id": 2, 
    "material": 4, 
    "noMaterial": 3, 
    "negative": 3
}, {
    "id": 3, 
    "material": 0, 
    "noMaterial": 1, 
    "negative": 1
}];


let keyName = ['material', 'noMaterial', 'negative'];
let [material, noMaterial, negative] = [0, 0, 0];

data.map((v,i)=>{
    material += v.material;
    noMaterial += v.noMaterial;
    negative += v.negative;
});

const max = Math.max(material, noMaterial, negative);
const index = [material, noMaterial, negative].indexOf(max);

console.log(keyName[index]+':'+max)

about 4 years ago · Juan Pablo Isaza Report

0

I defined a reusable hook that you can use on other attributes of your array as follows:

function getSum(keyName, data) {
  return {[keyName]: 
    data.reduce((acc, current) => {
      return acc + current[keyName]; 
    }, 0)
  };
}

and then call apply it on your data as follows:

getSum("noMaterial", data);

here is link for the code

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!