代码如下:
async componentDidMount() { let { data } = await axios.get(`${BaseUrl}/food/lists`); this.listData = data.data; this.setState({ list: this._buildTree(0) // 一级pid为0 }); } // 实现一维的对象数组转化为树形数组 _buildTree = (id) => { const data = this.listData.filter((item) => item.pid === id); data.forEach(item => { item.children = this._buildTree(item.id); }); return data; }
.