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

121
Visualizações
How to get mutli-count of elements in Object Array by using map or other iterating functions

I have an Object array looks like this:

const myArray = [
  { bet_line: "1",bet_type: 1, f1: true,  f2: true,  f3: true, f4: false, f5:false }
  { bet_line: "1",bet_type: 2, f1: false, f2: true,  f3: true, f4: true,  f5:false }
  { bet_line: "1",bet_type: 4, f1: true,  f2: false, f3: true, f4: true,  f5:false }
   ... ...
];

and What i want to get is:

const chartData = [
  {name:'F1', value:2}, 
  {name:'F2', value:2}, 
  {name:'F3', value:3}, 
  {name:'F4', value:2},
  {name:'F5', value:0}
];

Now, I know how to get each element of new array by using array function:

const f1 = myArray.filter((x,i) => { return x.f1; }).length
const f2 = myArray.filter((x,i) => { return x.f1; }).length
...

Actually there are more than 5 for F* element, like F1~F20, so to caculuate everytime is not suitable way. I want to find more simple way, Is there any way to do this by using Javascript Array function?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use a single reduce to "sum" the items. Because they're booleans, it's safe (in javascript...) to sum them using +:

const myArray = [
  { bet_line: "1",bet_type: 1, f1: true,  f2: true,  f3: true, f4: false, f5:false },
  { bet_line: "1",bet_type: 2, f1: false, f2: true,  f3: true, f4: true,  f5:false },
  { bet_line: "1",bet_type: 4, f1: true,  f2: false, f3: true, f4: true,  f5:false }
];


const sumBet = (b1, b2) => ({ f1: b1.f1 + b2.f1, f2: b1.f2 + b2.f2, f3: b1.f3 + b2.f3, f4: b1.f4 + b2.f4, f5: b1.f5 + b2.f5 });

const emptyBet = () => ({ f1: false, f2: false, f3: false, f4: false, f5: false });


console.log(
  myArray.reduce(sumBet, emptyBet())
)

If you want it to be more generic, you can extract the key definition:

const myArray = [
  { bet_line: "1",bet_type: 1, f1: true,  f2: true,  f3: true, f4: false, f5:false },
  { bet_line: "1",bet_type: 2, f1: false, f2: true,  f3: true, f4: true,  f5:false },
  { bet_line: "1",bet_type: 4, f1: true,  f2: false, f3: true, f4: true,  f5:false }
];

const sumKeys = ks => (a, b) => Object.fromEntries(ks.map(k => [k, a[k] + b[k]]));
const constKeys = (ks, c) => Object.fromEntries(ks.map(k => [k, c]));

const keys = Array.from(Array(5), (_, i) => `f${i + 1}`);
console.log(
  myArray.reduce(sumKeys(keys), constKeys(keys, 0))
)

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