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

173
Visualizações
Array of objects: Count occurrences of a unique property in JavaScript

Given a sample JSON array of objects, like this....

[
  {
    "firstName": "Bob",
    "lastName": "Smith",
    "city": "Preston",
    "st": "Florida",
    "age": 15
  },
  {
    "firstName": "Tom",
    "lastName": "Jones",
    "city": "Springfield",
    "st": "Illinois",
    "age": 34
  },
  {
    "firstName": "Mary",
    "lastName": "Hart",
    "city": "Miami",
    "st": "Florida",
    "age": 22
  },
  {
    "firstName": "Jenny",
    "lastName": "Dixon",
    "city": "Palm City",
    "st": "Florida",
    "age": 26
  }
]

What is the best way to get the number of occurrences based on the groupings of a particular property? So, let's say I want to produce a JSON object that has each unique state ("st") and the number of occurrences....

[
  {
    "st": "Illinois",
    "count": 1
  },
  {
    "st": "Florida",
    "count": 3
  }
]

I can do it manually using a for-let, looping through the array, tracking the values as I loop, etc. But I'm sure there's a more efficient way using ES6. Can you please help me out? Thanks.

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

0

You can use Array#reduce to keep a tally

let counts = json.reduce((b, a) => {
  let index = b.findIndex(j => j.st === a.st);
  if (index > -1) b[index].count++;
  else b.push({st: a.st, count: 1});
  return b;
}, [])

#UPDATE: As mentioned by @epascarello, there is a more efficient way to go about this, removing the findIndex loop and using Object.values

const results = Object.values(json.reduce((obj, item) => {
   obj[item.st] = obj[item.st] || { st: item.st, count: 0 };
   obj[item.st].count++;       
   return obj;}, {}))

let json = [{
    "firstName": "Bob",
    "lastName": "Smith",
    "city": "Preston",
    "st": "Florida",
    "age": 15
  },
  {
    "firstName": "Tom",
    "lastName": "Jones",
    "city": "Springfield",
    "st": "Illinois",
    "age": 34
  },
  {
    "firstName": "Mary",
    "lastName": "Hart",
    "city": "Miami",
    "st": "Florida",
    "age": 22
  },
  {
    "firstName": "Jenny",
    "lastName": "Dixon",
    "city": "Palm City",
    "st": "Florida",
    "age": 26
  }
]

let counts = json.reduce((b, a) => {
  let index = b.findIndex(j => j.st === a.st);
  if (index > -1) b[index].count++;
  else b.push({st: a.st, count: 1});
  return b;
}, [])

console.log(counts)

const results = Object.values(json.reduce((obj, item) => {
obj[item.st] = obj[item.st] || { st: item.st, count: 0 };
obj[item.st].count++;       
return obj;}, {}))

console.log(results)

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