mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-04 13:08:41 +08:00
27 lines
777 B
TypeScript
27 lines
777 B
TypeScript
import assign from 'object-assign';
|
|
export function flatArray(data: Object[] = [], childrenName = 'children') {
|
|
const result: Object[] = [];
|
|
const loop = (array) => {
|
|
array.forEach(item => {
|
|
const newItem = assign({}, item);
|
|
delete newItem[childrenName];
|
|
result.push(newItem);
|
|
if (item[childrenName] && item[childrenName].length > 0) {
|
|
loop(item[childrenName]);
|
|
}
|
|
});
|
|
};
|
|
loop(data);
|
|
return result;
|
|
}
|
|
|
|
export function treeMap(tree: Object[], mapper: Function, childrenName = 'children') {
|
|
return tree.map((node, index) => {
|
|
const extra = {};
|
|
if (node[childrenName]) {
|
|
extra[childrenName] = treeMap(node[childrenName], mapper, childrenName);
|
|
}
|
|
return assign({}, mapper(node, index), extra);
|
|
});
|
|
}
|