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

220
Visualizações
How group and count elements by lodash?

My data:

[{
    id: 1,
    name: 'foo'
  }, {
    id: 2,
    name: 'bar'
  }, {
    id: 1,
    name: 'foo'
  }
];

I want to do something like this:

[
  {
    name: "foo",
    id: 1,
    count: 2
  },
  {
    name: "bar",
    id: 2,
    count: 1
  }
]

Now i'm grouping the elements with groupBy by name.

Thanks for the help!

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

0

var source = [{
    id: 1,
    name: 'foo'
  }, {
    id: 2,
    name: 'bar'
  }, {
    id: 1,
    name: 'foo'
  }
];
  
var result = _(source)
    .groupBy('name')
    .map(function(items, name) {
      return {
        name: name,
        count: items.length
        }
     }).value();

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

about 4 years ago · Juan Pablo Isaza Relatório

0

It's not exactly what you asked for, but here is a simple method without using lodash. Here I'm using the ID of each element, but you can use anything that uniquely identifies it.

const objArray = [{
    id: 1,
    name: 'foo'
  }, {
    id: 2,
    name: 'bar'
  }, {
    id: 1,
    name: 'foo'
  }
];

const reduceAndCount  = (arr) => {
    const newMap = {};
    arr.forEach(el => {
        if (!newMap[el.id]) {
            el.count = 1
            return newMap[el.id] = el
        }
        newMap[el.id].count = newMap[el.id].count + 1
    })
    return Object.values(newMap)
}

const result = reduceAndCount(objArray)
console.log(result)

about 4 years ago · Juan Pablo Isaza Relatório

0

With deference to the answer provided here which uses 'lodash', as requested in the above question, the below points are observed:

  1. the 'groupBy' is fixed (ie, the 'name' field/column/prop) is used
  2. the result given includes the name & count, but the expected result needs the 'id' as well.

EDIT: Adding solution using lodash,

const arrayRaw = [{
  id: 1,
  name: 'foo'
}, {
  id: 2,
  name: 'bar'
}, {
  id: 1,
  name: 'foo'
}];

const groupBy = (col = 'name', arr = arrayRaw) => (
  _(arr).groupBy(col).map(it => ({
    ...it[0],
    count: it.length
  })).value()
);

console.log(groupBy());
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/1.2.1/lodash.min.js"></script>

In case, someone requires a solution without 'lodash', the below should be one possible implementation:

const arrayRaw = [{
  id: 1,
  name: 'foo'
}, {
  id: 2,
  name: 'bar'
}, {
  id: 1,
  name: 'foo'
}];

const groupBy = (col = 'name', arr = arrayRaw) => (
  Object.entries(arr.reduce((fin, itm, idx) => ({
    ...fin,
    [itm[col]]: (fin[itm[col]] || []).concat([idx])
  }), {})).map(([k, v]) => ({
    ...arr[v[0]],
    count: v.length
  }))
);

console.log(groupBy());

Approach / Explanation It is self-explanatory, however, should anyone require one - please comment and I shall add.

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