Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda