树状数据递归封装 js思路
复制代码 再控制台查看
<template>
<view class="content">
</view>
</template>
<script>
export default {
data() {
return {
list: [
{
name: '1',
children: [
{
name: '1-1',
children: [
{
name: '1-1-1',
children: [
{
name: '1-1-1-1'
},
{
name: '1-1-1-2'
}
]
},
{
name: '1-2',
children: null
},
{
name: '1-3',
children: []
},
{
name: '1-4'
}
]
},{
name:'1-2'
}
]
},
{
name: 2
}
]
};
},
onLoad() {
console.log(this.recursionData(this.list,'children'));
},
methods: {
recursionData(data, key) {
let arr = [];
for (let i = 0; i < data.length; i++) {
if (key in data[i]) {
if (data[i][key] && data[i][key].length > 0) {
data[i][key] = this.recursionData(data[i][key],key);
arr.push(data[i]);
}
} else {
arr.push(data[i]);
}
}
return arr;
}
}
};
</script>
<style>
</style>