为了账号安全,请及时绑定邮箱和手机立即绑定

通过子字符串 JavaScript 对数组元素进行分组

通过子字符串 JavaScript 对数组元素进行分组

慕后森 2022-07-01 16:19:42
我有一个这样的元素列表[{  "id": 1,  "id_team": "1.",  "name": "Name",}, {  "id": 2,  "id_team": "2.",  "name": "Name",}, {  "id": 3,  "id_team": "3.",  "name": "Name",}, {  "id": 4,  "id_team": "4.",  "name": "Name",}, {  "id": 5,  "id_team": "5.",  "name": "Name",}, {  "id": 6,  "id_team": "2.1.",  "name": "Name",}, {  "id": 7,  "id_team": "6.",  "name": "Name",}, {  "id": 8,  "id_team": "1.1.",  "name": "Name",}, {  "id": 9,  "id_team": "1.2.",  "name": "Name",}, {  "id": 10,  "id_team": "1.3.",  "name": "Name",}, {  "id": 11,  "id_team": "1.4.",  "name": "Name",}]如示例中所示,我在列id_team中有一些字符串,这些字符串位于数组同一列中的其他字符串内例如,主要团队将是1.然后1.1.等等1.2.将是其中的一部分1.所以结果一定是这样的[{  "id": 1,  "id_team": "1.",  "name": "Name",  "subteams": [{      "id": 8,      "id_team": "1.1.",      "name": "Name",    }, {      "id": 9,      "id_team": "1.2.",      "name": "Name",    }, {      "id": 10,      "id_team": "1.3.",      "name": "Name",    }, {      "id": 11,      "id_team": "1.4.",      "name": "Name",    },],}, {  "id": 2,  "id_team": "2.",  "name": "Name",  "subitems": [{      "id": 6,      "id_team": "2.1.",      "name": "Name",    },]}, {  "id": 3,  "id_team": "3.",  "name": "Name",  "subitems": [],}, {  "id": 4,  "id_team": "4.",  "name": "Name",  "subitems": [],}, {  "id": 5,  "id_team": "5.",  "name": "Name",  "subitems": [],}, {  "id": 7,  "id_team": "6.",  "name": "Name",  "subitems": [],},]可行吗?如果它更容易,我会使用 lodash。
查看完整描述

3 回答

?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

let data = [{

  "id": 1,

  "id_team": "1.",

  "name": "Name",

}, {

  "id": 2,

  "id_team": "2.",

  "name": "Name",

}, {

  "id": 3,

  "id_team": "3.",

  "name": "Name",

}, {

  "id": 4,

  "id_team": "4.",

  "name": "Name",

}, {

  "id": 5,

  "id_team": "1.2.5.",

  "name": "Name",

}, {

  "id": 6,

  "id_team": "2.1.",

  "name": "Name",

}, {

  "id": 7,

  "id_team": "6.",

  "name": "Name",

}, {

  "id": 8,

  "id_team": "1.1.",

  "name": "Name",

}, {

  "id": 9,

  "id_team": "1.2.",

  "name": "Name",

}, {

  "id": 10,

  "id_team": "1.3.",

  "name": "Name",

}, {

  "id": 11,

  "id_team": "1.4.",

  "name": "Name",

}];


data = data.map(node=>{

  let t = node.id_team.split('.').filter(n=>n);

  let parent = t.splice(0,t.length-1).join('.');

  return {

    ...node,

    parent: !!parent ? parent+'.' : null

  }

});


let tree = (items, id = null) =>

  items

    .filter(item => item.parent === id)

    .map(item => ({ ...item, subTeams: tree(items, item.id_team) }));



console.log(JSON.stringify(tree(data),null,2));


查看完整回答
反对 回复 2022-07-01
?
噜噜哒

TA贡献1784条经验 获得超7个赞

const items = data.reduce((acc, item, index) => {

    const id_team = item.id_team.split('.')[0] + '.';

    const current = acc.find(record => record.id_team === id_team);


    if(current) current.subteams.push(item);

    else acc.push({

        id: index + 1,

        id_team,

        subteams: [item]

    });


    return acc;

}, []);


查看完整回答
反对 回复 2022-07-01
?
一只萌萌小番薯

TA贡献1795条经验 获得超7个赞

从示例的代码中,我只是检查了子团队数组是否有数据,如果有,我使用带有递归函数的辅助函数并将其分配给子团队。


 getSubTeams(teams, size) {

      const filtered = teams.filter(item => item.id_team.length <= size);


      return filtered.map((item) => {

        let subteams = teams.filter((team) => team.id_team.indexOf(item.id_team) === 0 && item.id_team !== team.id_team);

        if (subteams.length > 0) {

          let aux = this.getSubTeams(subteams, size + 2);

          subteams = [

            ...aux,

          ];

        }

        return {

          ...item,

          subteams,

        };

      }) || [];

    },


查看完整回答
反对 回复 2022-07-01
  • 3 回答
  • 0 关注
  • 139 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信