在vue3中如何把数组对象中的name和id转换成lable和value

//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 === 

猜你喜欢

转载自blog.csdn.net/u010782109/article/details/131703944