数据组装问题(算法)

数据组装问题(算法)

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


回答:

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": [

]

}

]

}

]

}

]

}

]


回答:

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": "强制划扣"

}

]

}

]

}

]

}

]

以上是 数据组装问题(算法) 的全部内容, 来源链接: utcz.com/p/935747.html

回到顶部