0

I have an array of objects in the below form it has three properties serviceName, pool, and environment I want to group the object based on environment and at the same time need to concat the pools:

const data = [
  {
    serviceName: "visa",
    pool: "3g",
    environment: "test-int",
  },
  {
    serviceName: "visa",
    pool: "4g",
    environment: "test-int",
  },
  {
    serviceName: "visa",
    pool: "5g",
    environment: "test-int",
  },
  {
    serviceName: "amex",
    pool: "5g",
    environment: "dev",
  },
  {
    serviceName: "amex",
    pool: "6g",
    environment: "dev",
  },
];

I want the output in the below format:

const output = [
    {
      serviceName: "visa",
      pool: "3g,4g,5g",
      environment: "test-int"
    },
    {
      serviceName: "amex",
      pool: "5g,6g",
      environment: "dev"
    },
  ]

Based on my current code it just returns a single object instead of an array of objects:

const output = data.reduce((acc, ar) => {
      let res = {
        ...acc,
        pool: acc["pool"] + "," + ar.pool
      };
      return res;
    }
  });
3
  • What's not working here? Did you get console errors? Is this in a browser or backend? Please clarify where you are having problems. Commented Apr 24, 2022 at 22:57
  • @mrrogers i just joined StackOverflow so was figuring out how to format the code i have added the required details Commented Apr 24, 2022 at 23:11
  • pool is probably more useful as an array instead of a string. Also reduce() returns a single result as you've already experienced. Commented Apr 24, 2022 at 23:14

2 Answers 2

2

I think the basic problem is that you need to segment out all the elements that match by serviceName and then do your reduce to join the pool value.

Maybe something like this:

const res = [
    {
      serviceName: "visa",
      pool: "3g",
      environment: "test-int"
    },
    {
      serviceName: "visa",
      pool: "4g",
      environment: "test-int"
    },
    {
      serviceName: "amex",
      pool: "5g",
      environment: "dev"
    },
    {
      serviceName: "amex",
      pool: "6g",
      environment: "dev"
    }
  ];

const groupBy = (arr, getKey) => {
  return arr.reduce((memo, item) => {
    const key = getKey(item)
    memo[key] ||= [];
    memo[key].push(item);
    return memo;
    
  }, {})
}

const byServiceName = groupBy(res, ({serviceName}) => serviceName);

const results = Object.values(byServiceName).map((items) => {
   return items.reduce((acc, item) => {
     let res = {
        ...acc,
        pool: acc["pool"] + "," + item.pool
      };
      return res;
    })
});

console.log(results)

The groupBy function could be simplified to not take the getKey function, but that adds a little flexibility. If you know it's always going to be on serviceName, you could instead have something like

const groupByServiceName = (arr) => {
  const key = 'serviceName'
  return arr.reduce((memo, item) => {
    memo[key] ||= [];
    memo[key].push(item);
    return memo;
}, {})
Sign up to request clarification or add additional context in comments.

Comments

1

Here is a method that generates an object with environment value as key and then extracts its values into array

const data = [
  {
    serviceName: "visa",
    pool: "3g",
    environment: "test-int",
  },
  {
    serviceName: "visa",
    pool: "4g",
    environment: "test-int",
  },
  {
    serviceName: "visa",
    pool: "5g",
    environment: "test-int",
  },
  {
    serviceName: "amex",
    pool: "5g",
    environment: "dev",
  },
  {
    serviceName: "amex",
    pool: "6g",
    environment: "dev",
  },
];

const output = Object.values(data.reduce((acc, ar) =>
{
  if (acc[ar.environment])
    acc[ar.environment].pool += "," + ar.pool;
  else
    acc[ar.environment] = {...ar};

  return acc;
},{}));

console.log(output);

2 Comments

Interesting. Same as my solution except keyed by environment. I guess it's not clear, @giiyiraj, which thing is the "grouping" criteria. I assumed it was serviceName but I guess it could be either... or both.
The problem statement is "Given a list of services I want to group the services deployed based on environments and add the pools so basically as we see between the two same services the field that's different is the pool so wanted to reduce it to a single field instead of displaying multiple records in the UI"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.