1234567/** * @description 扁平数据树形化 * @param dataList 原数组 * @param options (keyId:id值,childrenName:子节点数组名,parentId:父id) * @param fixListItem 做其他操作 * @returns {[]} */ 1234567891011121314151617181920212223242526272829303132export function arrayToTree(dataList, options, fixListItem) { console.time('时间是:') const { keyId, childrenName, parentKeyId } = options// 解构赋值 const tree = [] const record = {} for (let i = 0, len = dataList.length; i < len; i++) { const dataItem = dataList[i] if (!valueIsBlank(fixListItem)) { fixListItem(dataItem) } const id = dataItem[keyId] if (!id) { continue// 中断循环中的迭代,如果出现了指定的条件,然后继续循环中的下一个迭代。 } if (record[id]) { dataItem[childrenName] = record[id] } else { dataItem[childrenName] = record[id] = [] } if (dataItem[parentKeyId]) { const parentId = dataItem[parentKeyId] if (!record[parentId]) { record[parentId] = [] } record[parentId].push(dataItem) } else { tree.push(dataItem) } } console.timeEnd('时间是:') return tree}