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

83
Vistas
Mapping count of unique IDs per project

I am receiving an Array of Objects like so

[
    {
       "payload":{
          "correlation":{
             "metadata":{
                "customerId":"12345",
                "project":"Project One"
             }
          }
       }
    },
    {
       "payload":{
          "correlation":{
             "metadata":{
                "customerId":"12345",
                "project":"Project Two"
             }
          }
       }
    },
    {
       "payload":{
          "correlation":{
             "metadata":{
                "customerId":"12345",
                "project":"Project Two"
             }
          }
       }
    },
    {
       "payload":{
          "correlation":{
             "metadata":{
                "customerId":"54323",
                "project":"Project One"
             }
          }
       }
    }
]

What I am trying to do is count all the unique customerId's per project. So for the above data, I would expect

Project One: 2
Project Two: 1 //because 12345 is shown twice for Project Two, we only want unique so count it as 1

So I can do the counts using a map, something like this

const lutProjects = new Map();
Object.entries(data).forEach(([key, value]) => {
  const project = value?.payload?.correlation?.metadata?.project;
  const custId = value?.payload?.correlation?.metadata?.customerId;
  
  lutProjects.set(project, (lutProjects.get(project) || 0) + 1);
});

However, this does not handle unique ids for each project, so outputs 2 and 2.

How can I also handle unique id's per project?

I have created a JSFiddle

Thanks

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can try quick fix with below snippet

let data = [{"payload":{"correlation":{"metadata":{"customerId":"12345","project":"Project One"}}}},{"payload":{"correlation":{"metadata":{"customerId":"12345","project":"Project Two"}}}},{"payload":{"correlation":{"metadata":{"customerId":"12345","project":"Project Two"}}}},{"payload":{"correlation":{"metadata":{"customerId":"54323","project":"Project One"}}}}]

data = Array.from(new Set(data.map(f => f.payload.correlation.metadata.project))).map(d => {
   return {
      [d]: [...new Set(data.filter(f => f.payload.correlation.metadata.project === d).map(c => c.payload.correlation.metadata.customerId))].length
   }
});

console.log(data);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use the reduce method in combination with an array of unique ids for each project, then use length on those array to know the final count. Here is my proposition:

const arr = [
    {
       "payload":{
          "correlation":{
             "metadata":{
                "customerId":"12345",
                "project":"Project One"
             } 
          }
       }
    },
    {
       "payload":{
          "correlation":{
             "metadata":{
                "customerId":"12345",
                "project":"Project Two"
             }
          }
       }
    },
    {
       "payload":{
          "correlation":{
             "metadata":{
                "customerId":"12345",
                "project":"Project Two"
             }
          }
       }
    },
    {
       "payload":{
          "correlation":{
             "metadata":{
                "customerId":"54323",
                "project":"Project One"
             }
          }
       }
    }
]

const cb = (accumulator, { payload }) => {
    const { project, customerId } = payload.correlation.metadata
    if (!accumulator[project]) {
      return { ...accumulator, [project]: [customerId] }
    }
    else if (accumulator[project] && accumulator[project].indexOf(customerId) === -1) {
    return { ...accumulator, [project]: [...accumulator[project], customerId] }
    } else return accumulator
}

const initialAccumulator = {}

const result1 = arr.reduce(cb, initialAccumulator)
const result2 = Object.keys(result1).map(key => ({ [key]: result1[key].length }))
console.log(result2)
about 4 years ago · Juan Pablo Isaza Denunciar

0

If you use reduce() to create an object with the project as key, and the value as an array containing the customerId, we can use includes() to check if this customerId is already known, and only add it to the array if not.

Then, we can use a second reduce() to change the array ow customerId's to the length of the array:

const data = [{"payload":{"correlation":{"metadata":{"customerId":"12345", "project":"Project One"} } } }, {"payload":{"correlation":{"metadata":{"customerId":"12345", "project":"Project Two"} } } }, {"payload":{"correlation":{"metadata":{"customerId":"12345", "project":"Project Two"} } } }, {"payload":{"correlation":{"metadata":{"customerId":"54323", "project":"Project One"} } } } ];

let res = data.reduce((prev, cur) => {
    const { customerId, project } = cur.payload.correlation.metadata;
    if (!prev[project]) prev[project] = [];
    if (!prev[project].includes(customerId)) {
      prev[project].push(customerId);
    }
    return prev;
}, {});
res = Object.keys(res).reduce((prev, cur) => ({ ...prev, [cur]: res[cur].length }), {});

console.log(res);

{
  "Project One": 2,
  "Project Two": 1
}
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