//1.方法一
//公共转换方法
export const replaceKeys= (data) => {
const transformedData = data.map(item => ({
value: item.id,
label: item.name,
parentId: item.parentId,
children: item.postList.map(post => ({
value: post.id,
label: post.name,
deptId: post.deptId,
dataScope: post.dataScope
}))
}));
return transformedData;
}
2.方法2
//公共转换方法把name转换成lable 把postList转换成children
export const replaceNameWithLabel= (data) => {
if (typeof data === 'object') {
if (Array.isArray(data)) {
return data.map(item => replaceNameWithLabel(item));
} else {
const newData = {};
for (const key in data) {
if (key === 'name') {
newData['label'] = data[key];
} else {
newData[key] = replaceNameWithLabel(data[key]);
}
if (key ===
在vue3中如何把数组对象中的name和id转换成lable和value
最新推荐文章于 2024-05-20 14:16:24 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)