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

0

Issue

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

Solution

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;

    }, {})
);

Answered By – Peter Seliger

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More