以下是我的数组,我需要用title替换键name,用subtitle替换键Email.

我try 了一些方法,但我仍然需要满足我的要求.请对此提供任何解决方案.

const newUpdatedList = [];
resArr.forEach((res) => {
  const obj = {
    title: res.name,
    subtitle: res.attributes.Email
  };

  if (res.children) {
    const newList = res.children.map((ch) => {
      return {
        title: ch.name,
        subtitle: ch.attributes.Email,
      };
    });
    obj.children = newList;
  }
  newUpdatedList.push(obj);
});
const resArr = 
  [ { user_id    : 'f7ba4795-d279-4c38-9a84-7a49522c50a2'
    , name       : 'Harsha ABC'
    , custom_id  : 'mani78989-1gfqv04bo'
    , attributes : { Email: 'harsha@gmail.com', Role: 'admin'} 
    , children: 
      [ { user_id    : 'd748037a-b445-41c2-b82f-4d6ee9396714'
        , name       : 'Lavaraju Allu'
        , custom_id  : 'mani78989-1gfqv472q'
        , attributes : { Email: 'raju@gmail.com', Role: 'Manager'} 
        , children: 
          [ { user_id    : '881c7731-b853-4ebc-b271-8f9e9215f7a1'
            , name       : 'Ramesh Allu'
            , custom_id  : 'mani78989-1gh14i13t'
            , attributes : { Email: 'ramesh@gmail.com', Role: 'Retailer'} 
            , children: 
              [ { user_id    : 'f7ba4795-d279-4c38-9a84-7a49522c50a2'
                , name       : 'Harsha ABC'
                , custom_id  : 'mani78989-1gh15nrev'
                , attributes : { Email: 'harsha@gmail.com', Role: 'Delivery Manager'} 
                , children   : [] 
        } ] } ] } 
      , { user_id    : '550cc296-d7e4-44fb-9d62-4c6755b3f6f2'
        , name       : 'Suresh Kunisetti'
        , custom_id  : 'mani78989-1gfqv6idi'
        , attributes : { Email: 'suresh@gmail.com', Role: 'Super Admin'} 
        , children: 
          [ { user_id    : '45cf19f8-36c1-4669-9333-1226c4f7b66b'
            , name       : 'Harish Three'
            , custom_id  : 'mani78989-1ggv5vffb'
            , attributes : { Email: 'harish234@gmail.com', Role: 'Delivery Manager'} 
            , children   : [] 
        } ] } 
      , { user_id    : '2c8535be-5fe7-40f0-892f-0f9bcffe0baa'
        , name       : 'Sandeep Bbb'
        , custom_id  : 'mani78989-1gh14m5p4'
        , attributes : { Email: 'sandeep@gmail.com', Role: 'Delivery Manager'} 
        , children   : [] 
        } 
      , { user_id    : '881c7731-b853-4ebc-b271-8f9e9215f7a1'
        , name       : 'Ramesh Allu'
        , custom_id  : 'mani78989-1gh14pc6p'
        , attributes : { Email: 'ramesh@gmail.com', Role: 'Manager'} 
        , children   : [ ] 
    } ] } 
  ] 

预期yields 为

const resArr = 
  [ { user_id    : 'f7ba4795-d279-4c38-9a84-7a49522c50a2'
    , title      : 'Harsha ABC'
    , custom_id  : 'mani78989-1gfqv04bo'
    , attributes : { subtitle: 'harsha@gmail.com', Role: 'admin'} 
    , children: 
      [ { user_id    : 'd748037a-b445-41c2-b82f-4d6ee9396714'
        , title      : 'Lavaraju Allu'
        , custom_id  : 'mani78989-1gfqv472q'
        , attributes : { subtitle: 'raju@gmail.com', Role: 'Manager'} 
        , children: 
          [ { user_id    : '881c7731-b853-4ebc-b271-8f9e9215f7a1'
            , title      : 'Ramesh Allu'
            , custom_id  : 'mani78989-1gh14i13t'
            , attributes : { subtitle: 'ramesh@gmail.com', Role: 'Retailer'} 
            , children: 
              [ { user_id    : 'f7ba4795-d279-4c38-9a84-7a49522c50a2'
                , title      : 'Harsha ABC'
                , custom_id  : 'mani78989-1gh15nrev'
                , attributes : { subtitle: 'harsha@gmail.com', Role: 'Delivery Manager'} 
                , children   : [] 
        } ] } ] } 
      , { user_id    : '550cc296-d7e4-44fb-9d62-4c6755b3f6f2'
        , title      : 'Suresh Kunisetti'
        , custom_id  : 'mani78989-1gfqv6idi'
        , attributes : { subtitle: 'suresh@gmail.com', Role: 'Super Admin'} 
        , children: 
          [ { user_id    : '45cf19f8-36c1-4669-9333-1226c4f7b66b'
            , title      : 'Harish Three'
            , custom_id  : 'mani78989-1ggv5vffb'
            , attributes : { subtitle: 'harish234@gmail.com', Role: 'Delivery Manager'} 
            , children   : [] 
        } ] } 
      , { user_id    : '2c8535be-5fe7-40f0-892f-0f9bcffe0baa'
        , title      : 'Sandeep Bbb'
        , custom_id  : 'mani78989-1gh14m5p4'
        , attributes : { subtitle: 'sandeep@gmail.com', Role: 'Delivery Manager'} 
        , children   : [] 
        } 
      , { user_id    : '881c7731-b853-4ebc-b271-8f9e9215f7a1'
        , title      : 'Ramesh Allu'
        , custom_id  : 'mani78989-1gh14pc6p'
        , attributes : { subtitle: 'ramesh@gmail.com', Role: 'Manager'} 
        , children   : [] 
    } ] } 
  ] 

推荐答案

这是一个递归解决方案.

const resArr= [{"user_id": "f7ba4795-d279-4c38-9a84-7a49522c50a2","name": "Harsha ABC","custom_id": "mani78989-1gfqv04bo","attributes": {"Email": "harsha@gmail.com","Role": "admin"},"children": [{"user_id": "d748037a-b445-41c2-b82f-4d6ee9396714","name": "Lavaraju Allu","custom_id": "mani78989-1gfqv472q","attributes": {"Email": "raju@gmail.com","Role": "Manager"},"children": [{"user_id": "881c7731-b853-4ebc-b271-8f9e9215f7a1","name": "Ramesh Allu","custom_id": "mani78989-1gh14i13t","attributes": {"Email": "ramesh@gmail.com","Role": "Retailer"},"children": [{"user_id": "f7ba4795-d279-4c38-9a84-7a49522c50a2","name": "Harsha ABC","custom_id": "mani78989-1gh15nrev","attributes": {"Email": "harsha@gmail.com","Role": "Delivery Manager"},"children": []}]}]},{"user_id": "550cc296-d7e4-44fb-9d62-4c6755b3f6f2","name": "Suresh Kunisetti","custom_id": "mani78989-1gfqv6idi","attributes": {"Email": "suresh@gmail.com","Role": "Super Admin"},"children": [{"user_id": "45cf19f8-36c1-4669-9333-1226c4f7b66b","name": "Harish Three","custom_id": "mani78989-1ggv5vffb","attributes": {"Email": "harish234@gmail.com","Role": "Delivery Manager"},"children": []}]},{"user_id": "2c8535be-5fe7-40f0-892f-0f9bcffe0baa","name": "Sandeep Bbb","custom_id": "mani78989-1gh14m5p4","attributes": {"Email": "sandeep@gmail.com","Role": "Delivery Manager"},"children": []},{"user_id": "881c7731-b853-4ebc-b271-8f9e9215f7a1","name": "Ramesh Allu","custom_id": "mani78989-1gh14pc6p","attributes": {"Email": "ramesh@gmail.com","Role": "Manager"},"children": []}]}]

function changeTitles(Obj){
  Obj.title = Obj.name;
  Obj.attributes.subtitle = Obj.attributes.Email;
  delete Obj.name;
  delete Obj.attributes.Email;
  if (Obj.children) {
    Obj.children.forEach(changeTitles)
  }
}

const clone = JSON.parse(JSON.stringify(resArr)) // Because the function mutates the object
clone.forEach(changeTitles)

console.log(clone)

Javascript相关问答推荐

当在select中 Select 选项时,我必须禁用不匹配的 Select

传递一个大对象以在Express布局中呈现

如何从JSON数组添加Google Maps标记—或者如何为数组添加参数到标记构造器

无法在nextjs应用程序中通过id从mongoDB删除'

如何为我的astro页面中的相同组件自动创建不同的内容?

有没有可能使滑动img动画以更快的速度连续?

不能将空字符串传递给cy.containes()

对网格项目进行垂直排序不起作用

未加载css colored颜色 ,无法将div设置为可见和不可见

搜索功能不是在分页的每一页上进行搜索

如何在HTMX提示符中设置默认值?

我想将Sitecore搜索面过滤器从多个转换为单个

将基元传递给THEN处理程序

如何组合Multer上传?

如何使用puppeteer操作所有选项

如何使用Cypress在IFRAME中打字

在Press Reaction本机和EXPO av上播放单个文件

使用props 将VUE 3组件导入JS文件

带元素数组的Mongo聚合

在特定区域的图像上 Select x和y,并在其上创建边框