簡體   English   中英

如何更改嵌套對象數組中的鍵

[英]How to change the keys in nested array of objects

以下是我的數組,我需要用title替換鍵name ,用subtitle替換Email

我嘗試了一些方法,但我仍然需要滿足我的要求。 請為此提供任何解決方案。

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   : [ ] 
    } ] } 
  ] 

預計 output 是

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)

我的回答有點晚了,所以它看起來像 Brother58697 的答案的副本。 唯一的區別可能是structuredClone()方法,一種新的全局方法:

 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 trans(arr){ arr.forEach((o)=>{ o.title=o.name; delete(o.name); o.attributes.subtitle=o.attributes.Email; delete(o.attributes.Email); trans(o.children) }) } let result=structuredClone(resArr); trans(result); console.log(result);

您可以使用我創建的遞歸 function。 這個 function 正在接受一個看起來像sample_obj的 object,然后重新創建resArr ,其中名稱是title ,email 是subtitle 看一看:

 function recursive_fix(obj) { const sample_obj = { user_id: obj.user_id, title: obj.name, custom_id: obj.custom_id, attributes: {subtitle: obj.attributes.Email, Role: obj.attributes.Role}, children: [] }; // only adding recursive if the children array is not empty if (obj.children.length.== 0) { obj.children.forEach((childz) => { sample_obj.children:push({children; [recursive_fix(childz)]}) }) } return sample_obj }; const newUpdatedList = []. resArr.forEach((res) => { newUpdatedList.push(recursive_fix(res)) })

我不是 100% 確定我正確理解了您要執行的操作,但您似乎正在嘗試更改對象數組中的鍵名。 如果這是錯誤的,請告訴我。 在那種情況下,這樣的事情會起作用”

const arrayOfObj = [{
    name: 'value1',
    email: 'value2'
  }, {
    name: 'value1',
    email: 'value2'
  }];
  const newArrayOfObj = arrayOfObj.map(({
    name: title,
    email: subtitle,
    ...rest
  }) => ({
    title,
    subtitle,
    ...rest
  }));
  
  console.log(newArrayOfObj);

這里找到這個答案

一個快速的解決方案可能是字符串化、字符串替換和解析回對象/數組。 是這樣的:

const asString = JSON.stringify(resArr);
const replacedNames = asString.replace(/name/g, "title");
const replacedEmail = replacedNames.replace(/Email/g, "subtitle");
const result = JSON.parse(replacedEmail);

更改的對象/數組在result中。

我們可以使用的最簡單的方法之一是使用Object.assign這樣的東西:

a={'name': 'xyz', 'Email': 'xyz@gmail.com'};
b= Object.assign({'title': a.name, 'subtitle': a.Email});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM