编辑代码


/**
 * 树型结构数据集 
 */
let titleTree = [
    {
        "id": 1,
        "title": "标题1",
        "parent_id": 0
    },
    {
        "id": 2,
        "title": "标题2",
        "parent_id": 0,
        "children": [
            {
                "id": 3,
                "title": "标题2-1",
                "parent_id": 2,
                "children": [
                    {
                        "id": 4,
                        "title": "标题3-1",
                        "parent_id": 3,
                        "children": [
                            {
                                "id": 5,
                                "title": "标题4-1",
                                "parent_id": 4
                            }
                        ]
                    }
                ]
            },
            {
                "id": 6,
                "title": "标题2-2",
                "parent_id": 2
            }
        ]
    }
]



/**
 * 树型转二维扁平数组函数
 * @author WEI.ZHOU
 * @version V1.0.0
 */
function flatter(data){
	return data.reduce((pre, cur) => {
		const {id, title, parent_id, children = [] } = cur;
		return pre.concat([{id, title, parent_id}], flatter(children));
	}, []);
}

/**
 * 测试方法
 */
let titleList = flatter(titleTree);
console.log(titleList)