后台返回数据格式
let treeList = [
{
id: '01',
name: '货品类别',
children: [
{
id: '01-1',
pkey: '01',
name: '类别A',
children: [
{
id: '01-1-1',
pkey: '01-1',
name: '类别A1',
data: {
count: '1',
unit: '个',
goodCode: 'A001',
goodName: '刀片',
type: '1盒6片',
memo: '无',
},
},
{
id: '01-1-2',
pkey: '01-1',
name: '类别A2',
data: {
count: '1',
unit: '个',
goodCode: 'A001',
goodName: '刀片',
type: '1盒9片',
memo: '无',
},
},
{
id: '01-1-3',
pkey: '01-1',
name: '类别A3',
data: {
count: '1',
unit: '个',
goodCode: 'A001',
goodName: '刀片',
type: '1盒8片',
memo: '无',
},
},
],
},
{
id: '01-2',
pkey: '01',
name: '类别B',
children: [
{
id: '01-2-1',
pkey: '01-2',
name: '类别B1',
data: {
count: '1',
unit: '个',
goodCode: 'A001',
goodName: '刀片',
type: '1盒11片',
memo: '无',
},
},
{
id: '01-2-2',
pkey: '01-2',
name: '类别B2',
data: {
count: '1',
unit: '个',
goodCode: 'A001',
goodName: '刀片',
type: '1盒1片',
memo: '无',
},
},
{
id: '01-2-3',
pkey: '01-2',
name: '类别B3',
data: {
count: '1',
unit: '个',
goodCode: 'A001',
goodName: '刀片',
type: '1盒10片',
memo: '无',
},
},
],
},
{
id: '01-3',
pkey: '01',
name: '类别C',
children: [
{
id: '01-3-1',
pkey: '01-3',
name: '类别C1',
data: {
count: '1',
unit: '个',
goodCode: 'A001',
goodName: '刀片',
type: '1盒40片',
memo: '无',
},
},
{
id: '01-3-2',
pkey: '01-3',
name: '类别C2',
data: {
count: '1',
unit: '个',
goodCode: 'A001',
goodName: '刀片',
type: '一盒30片',
memo: '无',
},
},
{
id: '01-3-3',
pkey: '01-3',
name: '类别C3',
data: {
count: '1',
unit: '个',
goodCode: 'A001',
goodName: '刀片',
type: '1盒20片',
memo: '无',
},
},
],
},
],
},
{
id: '02',
name: '货品类别',
children: [
{
id: '02-1',
pkey: '02',
name: '类别A',
children: [
{
id: '02-1-1',
pkey: '02-1',
name: '类别A1',
data: {
count: '1',
unit: '个',
goodCode: 'A001',
goodName: '刀片',
type: '1盒6片',
memo: '无',
},
},
{
id: '02-1-2',
pkey: '02-1',
name: '类别A2',
data: {
count: '1',
unit: '个',
goodCode: 'A001',
goodName: '刀片',
type: '1盒9片',
memo: '无',
},
},
{
id: '02-1-3',
pkey: '02-1',
name: '类别A3',
data: {
count: '1',
unit: '个',
goodCode: 'A001',
goodName: '刀片',
type: '1盒8片',
memo: '无',
},
},
],
},
{
id: '02-2',
pkey: '02',
name: '类别B',
children: [
{
id: '02-2-1',
pkey: '02-2',
name: '类别B1',
data: {
count: '1',
unit: '个',
goodCode: 'A001',
goodName: '刀片',
type: '1盒11片',
memo: '无',
},
},
{
id: '02-2-2',
pkey: '02-2',
name: '类别B2',
data: {
count: '1',
unit: '个',
goodCode: 'A001',
goodName: '刀片',
type: '1盒1片',
memo: '无',
},
},
{
id: '02-2-3',
pkey: '02-2',
name: '类别B3',
data: {
count: '1',
unit: '个',
goodCode: 'A001',
goodName: '刀片',
type: '1盒10片',
memo: '无',
},
},
],
},
{
id: '02-3',
pkey: '02',
name: '类别C',
children: [
{
key: '02-3-1',
pkey: '02-3',
title: '类别C1',
data: {
count: '1',
unit: '个',
goodCode: 'A001',
goodName: '刀片',
type: '1盒40片',
memo: '无',
},
},
{
id: '02-3-2',
pkey: '02-3',
name: '类别C2',
data: {
count: '1',
unit: '个',
goodCode: 'A001',
goodName: '刀片',
type: '一盒30片',
memo: '无',
},
},
{
id: '02-3-3',
pkey: '02-3',
name: '类别C3',
data: {
count: '1',
unit: '个',
goodCode: 'A001',
goodName: '刀片',
type: '1盒20片',
memo: '无',
},
},
],
},
],
}
]
递归替换参数id为key,name为title
function replaceNameAndKey(data) {
data = data.map(item => {
return {
// ...item, // 如果想在原数组添加属性
title: item.name,
key: item.id,
children: item.children ? replaceNameAndKey(item.children) : item.children // 判断当前是否还有子节点
}
})
return data
}
-
结果
漂亮