function getParentLabelByValue(
array: Array<any>,
id: string|number,
fieldNames: FilesName = { value: 'id', label: 'name', children: 'children' }
): Array<string> {
const stack = [];
let going = true;
const walker = (array, id) => {
array.forEach((item: any) => {
if (!going) return;
stack.push(item[fieldNames.label]);
if (item[fieldNames.value] === id) {
going = false;
} else if (item[fieldNames.children]) {
walker(item[fieldNames.children], id);
} else {
stack.pop();
}
});
if (going) stack.pop();
};
walker(array, id);
return stack;
}
网友评论