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

342
Vistas
How can I get keys with unique values in an array of objects? (JavaScript)

I have an array of Objects containing two values: {path: '/index', ip: '123.456.789'} Some of the paths are the same, as are the ip's. Some duplicates, some unique combinations.

I want, for each unique path, the number of different ip's attached to that path. So, for example, there may be 15 Objects with path: '/index', but only 4 unique ip's for that path.

In simpler terms, I want to find the amount of unique visitors to a particular website page.

Hope this makes sense, many thanks in advance

Edit:

Here is what I have so far, to generate non-unique views:

export const generateViews = (viewData: string): Map<string, number> => {
  const pathViewMap: Map<string, number> = new Map();
  const viewDataArray = viewData.split("\n");
  for (let i = 0; i < viewDataArray.length; i++) {
    const [path] = viewDataArray[i].split(" ");
    if (path) {
      if (pathViewMap.has(path)) {
        pathViewMap.set(path, pathViewMap.get(path) + 1);
      } else {
        pathViewMap.set(path, 1);
      }
    }
  }

  return pathViewMap;
};

For context, the input is a string that comes from a log file of a list of paths/ips

Edit 2:

Thanks to Peter Seliger, I've been able to come up with my own solution:

const viewDataArray = viewData.split("\n").filter((item) => item);
  const arr: { path: string; ip: string }[] = viewDataArray.map(
    (line: string) => {
      const [path, ip] = line.split(" ");
      if (path && ip) {
        return { path, ip };
      }
    }
  );
  const paths: string[] = Array.from(new Set(arr.map((obj) => obj.path)));
  const uniqueViewsMap: Map<string, number> = new Map();

  for (let i = 0; i < paths.length; i++) {
    const path = paths[i];
    const ips = Array.from(
      new Set(arr.filter((obj) => obj.path === path).map((obj) => obj.ip))
    );
    uniqueViewsMap.set(path, ips.length);
  }

  console.log("==uniqueViewsMap==", uniqueViewsMap);
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

const sampleData = [
  { path: '/index', ip: '123.456.789' },
  { path: '/index/x', ip: '123.456.789' },
  { path: '/index/', ip: '123.456.78' },
  { path: '/index/y', ip: '123.456.789' },
  { path: '/index/', ip: '123.456.89' },
  { path: 'index/', ip: '123.456.9' },
  { path: 'index', ip: '123.456.8' },
  { path: '/index/', ip: '123.456.78' },
  { path: '/index/x/', ip: '123.456.78' },
  { path: 'index/x/', ip: '123.456.7' },
  { path: 'index/x', ip: '123.456.6' },
];
console.log(
  sampleData
    .reduce((result, { path, ip }, idx, arr) => {

      // sanitize/unify any path value
      path = path.replace(/^\/+/, '').replace(/\/+$/, '');

      // access and/or create a path specific
      // set and add the `ip` value to it.
      (result[path] ??= new Set).add(ip);

      // within the last iteration step
      // transform the aggregated object
      // into the final result with the
      // path specific unique ip count.      
      if (idx === arr.length - 1) {
        result = Object
          .entries(result)
          .reduce((obj, [path, set]) =>
            Object.assign(obj, {
              [path]: set.size
            }), {}
          );
      }
      return result;

    }, {})
);

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