Group objects by name Ramda

177 Views Asked by At

Good evening, I need to group an array of objects by their nickname, here I show you the data:

[
  {
    "nickName": "Info2",
    "countNotice": 4
  },
  {
    "nickName": "Info2",
    "countAlarm": 1
  }, 
  {
    "nickName": "Info1",
    "countNotice": 2
  },
  {
    "nickName": "Info1",
    "countAlarm": 3
  }
]

here I show you the expected result:

[{
  "nickName": "Info1",
  "countNotice": 2,
  "countAlarm": 3
},
{
  "nickName": "Info1",
  "countAlarm": 1,
  "countNotice": 4
}]

I tried aggregate from MongoDB in NodeJs but it was impossible.

2

There are 2 best solutions below

1
On

You could group and then merge each grouped

const res = R.compose(
  R.map(R.mergeAll),
  R.values,
  R.groupBy(R.prop("nickName"))
)(data)

Live example

const data = [
  {
    nickName: "Info2",
    countNotice: 4,
  },
  {
    nickName: "Info2",
    countAlarm: 1,
  },
  {
    nickName: "Info1",
    countNotice: 2,
  },
  {
    nickName: "Info1",
    countAlarm: 3,
  },
]

const res = R.compose(
  R.map(R.mergeAll),
  R.values,
  R.groupBy(R.prop("nickName"))
)(data)

console.log(res)
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>

0
On

Using Array.prototype.reduce, you can group array elements by nickName.

const input = [
  {
    "nickName": "Info2",
    "countNotice": 4
  },
  {
    "nickName": "Info2",
    "countAlarm": 1
  }, 
  {
    "nickName": "Info1",
    "countNotice": 2
  },
  {
    "nickName": "Info1",
    "countAlarm": 3
  }
];

const groupBy = input.reduce((acc, cur) => {
  acc[cur.nickName] ? acc[cur.nickName] = {
    ...acc[cur.nickName],
    ...cur
  } : acc[cur.nickName] = cur;
  return acc;
}, {});
const output = Object.values(groupBy);
console.log(output);