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 do I traverse an array of objects and find the number of times and element name occurs using Lodash

Related to my other question, where the image is How do I get rid of PROTOTYPE in my new JSON Object from an Array?

I want to be able to do the following:

Taking this one step further...

Each of those MARKET (name, displayName) which are both the same for now, have from 1 to "n" number of items within them. enter image description here

So, I want to COUNT how many ITEMS are within or repeated.

For example:

NAME: "IL" may have 300 
NAME: "WA" may have 1000 
NAME: "OR" may have 100 

and so on.

Inside these objects, the word MARKET is there. Market is what's above: IL, WA, CA, and so on are MARKETS like so:

[
    {
      ID: 1,
      NAME: "SomeName1",
      MARKET: "IL",
      LOCATION: "6 Miles east"
    },
    {
      ID: 2,
      NAME: "SomeName2",
      MARKET: "IL",
      LOCATION: "36 Miles east"
    },
    {
      ID: 3,
      NAME: "SomeName3",
      MARKET: "WA",
      LOCATION: "3 Miles west"
    },
    {
      ID: 4,
      NAME: "SomeName4",
      MARKET: "WA",
      LOCATION: "33 Miles west"
    },
    {
      ID: 5,
      NAME: "SomeName5",
      MARKET: "OR",
      LOCATION: "23 Miles north"
    },
    ...
]

I want to add a count to the MAP function I received as a solution:

const newObj = newMarketArray.map(e => ({ name: e, displayName: e }));

So that when the values appear as in the image above, I can get the number of occurrences of, WA, IL and OR for example.

The final JSON should have an additional element:

   displayName: WA,
   name: WA,
   count: 33

And finally, I want to SORT the values of the JSON object which is probably very easy.

UPDATE: A question was raised that the MAP

const newObj = newMarketArray.map(e => ({ name: e, displayName: e })); 

is false. That's not true. It returns the values in the IMAGE. So that is most certainly not false.

All I need is to COUNT the number of instances say, CA appears and place it like so:

const newObj = newMarketArray.map(e => ({ name: e, displayName: e, count: somecount }));

FYI: newMarketArray contains 3300+ objects

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

0

If i have well understood what you want:

newMarketArray = [
    {
      ID: 1,
      NAME: "SomeName1",
      MARKET: "IL",
      LOCATION: "6 Miles east"
    },
    {
      ID: 2,
      NAME: "SomeName2",
      MARKET: "IL",
      LOCATION: "36 Miles east"
    },
    {
      ID: 3,
      NAME: "SomeName3",
      MARKET: "WA",
      LOCATION: "3 Miles west"
    },
    {
      ID: 4,
      NAME: "SomeName4",
      MARKET: "WA",
      LOCATION: "33 Miles west"
    },
    {
      ID: 5,
      NAME: "SomeName5",
      MARKET: "OR",
      LOCATION: "23 Miles north"
    }
]

let objCount = newMarketArray.reduce((acc, obj) => {
    acc[obj.MARKET] = (acc[obj.MARKET] || 0) + 1;
    return acc;
}, {});
console.log(objCount);

let arrResult = Object.keys(objCount).map(k => ({'name': k, 'displayName': k, 'count': objCount[k] }));

console.log(arrResult)

After that, you could sort as you want the dictionary (by the value you want..)

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