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

164
Views
Encontrar el recuento más alto mediante la suma de un atributo en una matriz Json en javascript

Estoy buscando una forma limpia y eficiente de obtener el recuento de valor más alto mediante la suma de todos los atributos en la siguiente matriz json.

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

Rendimiento esperado:

{ "noMaterial": 7 }

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

0

Esta no es la manera perfecta, pero puede ser de ayuda para usted.

 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

Definí un enlace reutilizable que puede usar en otros atributos de su matriz de la siguiente manera:

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

y luego llame a aplicarlo en sus datos de la siguiente manera:

 getSum("noMaterial", data);

aquí hay un enlace para el código

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!