多层次groupby ramda js [英] multi level groupby ramda js

查看:50
本文介绍了多层次groupby ramda js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在多个级别上使用分组依据,以便按标记,文件夹和报告键对对象进行分组.我能够在单个级别进行分组,但无法继续进行.

I have been trying to use group by at multiple levels so that the object is grouped by tag, folder and report keys. I was able to groupby at single level but could not proceed further.

const data = [{
  "_index": "search_index",
  "_type": "search_type",
  "_id": "Co5EFxnqeo9ruq",
  "_score": 1,
  "_source": {
    "admin_tags": [],
    "type": "human",
    "folder": "Feature Samples",
    "url_t": "url_tdata",
    "users": [
      128
    ],
    "agent_id": 2,
    "url": "url_data",
    "favorited_by": [20],
    "idx": 121,
    "path": "Report Samples//Feature Samples",
    "name": "Grouping and Sorting",
    "description": ""
  }
}];

const mapIndexed = R.addIndex(R.map);

const tg = mapIndexed((x, i) => ({
  "name": x._source.admin_tags[0] == undefined ? "Unclasified" : x._source.admin_tags[0],
  "idx": i,
  "folder": [{
    "name": x._source.folder,
    "idx": x._source.folder,
    "report": [{
      "agent_id": x._source.agent_id,
      "description": x._source.description,
      "favorited_by": x._source.favorited_by[0],
      "name": x._source.name,
      "idx": x._source.idx,
      "path": x._source.path,
      "type": x._source.type,
      "url": x._source.url,
      "url_t": x._source.url_t,
    }]
  }]
}), data);

const byTag = R.groupBy(data => data.name)
const tagged = byTag(tg);
console.log(tagged)

<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.24.1/ramda.min.js"></script>

这是我试图通过对三个级别和user_report_id值进行分组来实现的输出,视图始终设置为零.

This is the output I am trying to achieve with grouping at three levels and values of user_report_id, views always set to zero.

const output = {
  "message": "",
  "payload": [{
    "name": "Unclasified",
    "idx": 0,
    "folders": [{
      "idx": "Feature Samples",
      "name": "Feature Samples",
      "reports": [{
        "agent_id": 2,
        "description": "",
        "idx": 121,
        "is_fav": 20,
        "name": "Grouping and Sorting",
        "type": "human",
        "url": "url_data",
        "url_t": "url_tdata",
        "user_report_id": 0,
        "views": 0,
        "report_id": '2sdfsd'
      }]
    }]
  }]
}

推荐答案

也许有更好的/内置的解决方案,但是您可以使用 map 将对象的内部列表"分组所以:

There might be a better/built in solution, but you can use map to group the "inner lists" of your objects like so:

const data = [
  { letter: "a", number: 1, bool: true },
  { letter: "a", number: 2, bool: true },
  { letter: "a", number: 2, bool: false },
  { letter: "b", number: 1, bool: true },
  { letter: "b", number: 1, bool: false },
  { letter: "b", number: 1, bool: false },
  { letter: "c", number: 1, bool: true },
  { letter: "c", number: 2, bool: true },
  { letter: "c", number: 2, bool: false },
  { letter: "c", number: 2, bool: true },
];

const groupByLetterNumberBool = pipe(
  groupBy(prop("letter")),
  map(groupBy(prop("number"))),
  map(map(groupBy(prop("bool"))))
);

groupByLetterNumberBool(data);

工作示例 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆