Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

223
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda