function Fn(arr: [], newKey: string, olKey: string) {
arr.forEach((item: any) => {
// 存在子级
if (item.children && item.children.length > 1) {
item[newKey] = item[olKey];
delete item[olKey];
Fn(item.children, newKey, olKey); // 递归调用
} else {
// 没有子级
item[newKey] = item[olKey];
delete item[olKey];
}
});
return arr;
}
export function getData(list: [], obj: any) {
var keys = Object.keys(obj); //获得对象的所有键名
keys.forEach((item) => {
list = Fn(list, obj[item], item);
});
return list;
}
JS递归修改键名
最新推荐文章于 2024-10-10 17:39:22 发布