猿问

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

我有一个这样的元素列表


[{

  "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。


慕后森
浏览 140回答 3
3回答

摇曳的蔷薇

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

噜噜哒

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;}, []);

一只萌萌小番薯

从示例的代码中,我只是检查了子团队数组是否有数据,如果有,我使用带有递归函数的辅助函数并将其分配给子团队。&nbsp;getSubTeams(teams, size) {&nbsp; &nbsp; &nbsp; const filtered = teams.filter(item => item.id_team.length <= size);&nbsp; &nbsp; &nbsp; return filtered.map((item) => {&nbsp; &nbsp; &nbsp; &nbsp; let subteams = teams.filter((team) => team.id_team.indexOf(item.id_team) === 0 && item.id_team !== team.id_team);&nbsp; &nbsp; &nbsp; &nbsp; if (subteams.length > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let aux = this.getSubTeams(subteams, size + 2);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subteams = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...aux,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...item,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subteams,&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; }) || [];&nbsp; &nbsp; },
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答