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

341
Visualizações
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 Respostas
Responde à pergunta

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 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