简体   繁体   中英

Group objects by name Ramda

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.

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM