数据组装问题(算法)

发布于 2022-09-13 00:41:50 字数 1666 浏览 31 评论 0

const res1 = [
            {
                first_scene: '支付',
                second_scene: '代发预扣',
                third_scene: '企业代发',
                forth_scene: '代发预扣'
            },
            {
                first_scene: '支付',
                second_scene: '代发转入',
                third_scene: '企业代发',
                forth_scene: '代发转入'
            },
            {
                first_scene: '支付',
                second_scene: '快捷提现',
                third_scene: '企业收款',
                forth_scene: '代扣转入'
            },
            {
                first_scene: '支付',
                second_scene: '快捷提现',
                third_scene: '企业收款',
                forth_scene: '汇款转入'
            },
            {
                first_scene: '理财',
                second_scene: '存入',
                third_scene: '智能存款',
                forth_scene: '存入'
            },
            {
                first_scene: '理财',
                second_scene: '强制划扣',
                third_scene: '个人账户',
                forth_scene: '强制划扣'
            },
        ];

就这么个数组,我想组装成这样的结果为:
即:去重且嵌套的组装数据。目前每个对象是是4个字段(first,second...forth),真实数据不固定,可能三个可能五个,希望支持根据参数来组装数据。

[
{
  name: 支付,
  children: [
   { 
    name: 代发预扣, 
    children: [{ name: 企业代发, children: [{ name: 代发转入 }]}]
   },
   { name:代发转入 },
   {
     name: 快捷提现,
     children:[{ name: 企业收款 }]
   },
 ]
},
{
 name: 理财,
 ...等等
]

感谢大佬,我脑瓜子嗡嗡的现在。
我的想法是一个函数,这样传参:

 // restParams 接收对象的字段
 function test(data, ...restParams){} 
 // 调用, 根据实际情况传参
 test(data, 'first_scene', 'second_scene', 'third_scene', 'forth_scene') 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赠我空喜 2022-09-20 00:41:50
var res1 = [
    {
        first_scene: '支付',
        second_scene: '代发预扣',
        third_scene: '企业代发',
        forth_scene: '代发预扣'
    },
    {
        first_scene: '支付',
        second_scene: '代发转入',
        third_scene: '企业代发',
        forth_scene: '代发转入'
    },
    {
        first_scene: '支付',
        second_scene: '快捷提现',
        third_scene: '企业收款',
        forth_scene: '代扣转入'
    },
    {
        first_scene: '支付',
        second_scene: '快捷提现',
        third_scene: '企业收款',
        forth_scene: '汇款转入'
    },
    {
        first_scene: '理财',
        second_scene: '存入',
        third_scene: '智能存款',
        forth_scene: '存入'
    },
    {
        first_scene: '理财',
        second_scene: '强制划扣',
        third_scene: '个人账户',
        forth_scene: '强制划扣'
    },
];

// restParams 接收对象的字段
function test(data, ...restParams) {
    if (restParams.length) {
        var key = restParams.shift();
        return Object.entries(data.reduce((acc, cur) => ((acc[cur[key]] ||= []).push(cur), acc), {})).map(([name, children]) => ({
            name,
            children: test(children, ...restParams)
        }));
    } else {
        return [];
    }
}

结果

[
  {
    "name": "支付",
    "children": [
      {
        "name": "代发预扣",
        "children": [
          {
            "name": "企业代发",
            "children": [
              {
                "name": "代发预扣",
                "children": [
                  
                ]
              }
            ]
          }
        ]
      },
      {
        "name": "代发转入",
        "children": [
          {
            "name": "企业代发",
            "children": [
              {
                "name": "代发转入",
                "children": [
                  
                ]
              }
            ]
          }
        ]
      },
      {
        "name": "快捷提现",
        "children": [
          {
            "name": "企业收款",
            "children": [
              {
                "name": "代扣转入",
                "children": [
                  
                ]
              },
              {
                "name": "汇款转入",
                "children": [
                  
                ]
              }
            ]
          }
        ]
      }
    ]
  },
  {
    "name": "理财",
    "children": [
      {
        "name": "存入",
        "children": [
          {
            "name": "智能存款",
            "children": [
              {
                "name": "存入",
                "children": [
                  
                ]
              }
            ]
          }
        ]
      },
      {
        "name": "强制划扣",
        "children": [
          {
            "name": "个人账户",
            "children": [
              {
                "name": "强制划扣",
                "children": [
                  
                ]
              }
            ]
          }
        ]
      }
    ]
  }
]
千秋岁 2022-09-20 00:41:50
function group(nameList, item, result = []) {
    let findArray = result;
    let keyLen = nameList.length;
    for (let i = 0; i < keyLen; i++) {
        const key = nameList[i];
        const name = item[key];
        const find = findArray.find(value => value.name === item[key]);
        if (find) {
            findArray = find.children;
        } else {
            if (i+1<keyLen){
                const children = [];
                findArray.push({name, children:children});
                findArray = children;
            }else{
                findArray.push({name});
            }

        }
    }
}

function groupArray(nameList, arr) {
    let result = [];
    for (let i = 0; i < arr.length; i++) {
        group(nameList, arr[i], result);
    }
    return result;
}
groupArray(['first_scene', 'second_scene', 'third_scene', 'forth_scene'], res1);
[
    {
        "name": "支付",
        "children": [
            {
                "name": "代发预扣",
                "children": [
                    {
                        "name": "企业代发",
                        "children": [
                            {
                                "name": "代发预扣"
                            }
                        ]
                    }
                ]
            },
            {
                "name": "代发转入",
                "children": [
                    {
                        "name": "企业代发",
                        "children": [
                            {
                                "name": "代发转入"
                            }
                        ]
                    }
                ]
            },
            {
                "name": "快捷提现",
                "children": [
                    {
                        "name": "企业收款",
                        "children": [
                            {
                                "name": "代扣转入"
                            },
                            {
                                "name": "汇款转入"
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "name": "理财",
        "children": [
            {
                "name": "存入",
                "children": [
                    {
                        "name": "智能存款",
                        "children": [
                            {
                                "name": "存入"
                            }
                        ]
                    }
                ]
            },
            {
                "name": "强制划扣",
                "children": [
                    {
                        "name": "个人账户",
                        "children": [
                            {
                                "name": "强制划扣"
                            }
                        ]
                    }
                ]
            }
        ]
    }
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文