javascript 如何通过嵌套多维数组Map并转换为新的响应

sz81bmfz  于 2023-03-16  发布在  Java
关注(0)|答案(2)|浏览(257)

你好,朋友们,假设我下面有嵌套的数据

const payload = [
    {
      title: "ADDITIONAL",
      data: [
        {
          documentCategory: "ADDITIONAL",
          documentCode: "CUSTOMER_CLAIM_FORM",
          documentKey:
            "customer/9fdf7ca8-4251-41c8-9116-6d006fa64aea/800123000004054/LIFESAVER_DEATH/LIFESAVER_DEATH_CUSTOMER_CLAIM_FORM_ca2b0204-b8c4-42f7-b059-580818789a64.jpg"
        },
        {
          documentCategory: "ADDITIONAL",
          documentCode: "POWER_OF_ATTORNEY",
          documentKey:
            "customer/9fdf7ca8-4251-41c8-9116-6d006fa64aea/800123000004054/LIFESAVER_DEATH/LIFESAVER_DEATH_POWER_OF_ATTORNEY_6de88371-c666-4ce5-87f2-8aedf778f4c4.jpg"
        },
        {
          documentCategory: "ADDITIONAL",
          documentCode: "POLICE_RECORD",
          documentKey:
            "customer/9fdf7ca8-4251-41c8-9116-6d006fa64aea/800123000004054/LIFESAVER_DEATH/LIFESAVER_DEATH_POLICE_RECORD_15d989de-9cfb-4574-8451-92f445177331.jpg"
        }
      ],
      chevronFlag: true
    },
...
]

我试过下面几种方法

for (let i = 0; i < payload.length; i++) {
    const element = payload[i].data.map((item) => {
      return item;
    });
    console.log(element);
  }

我得到了数组中每个属性的每个新对象的结果
enter image description here
我的目标是

const newObj = {
documentCode: "CUSTOMER_CLAIM_FORM"
documentKey: "customer/9fdf7ca8-4251-41c8-9116-6d006fa64aea/800123000004054/LIFESAVER_DEATH/LIFESAVER_DEATH_CUSTOMER_CLAIM_FORM_ca2b0204-b8c4-42f7-b059-580818789a64.jpg",
documentCode: "POWER_OF_ATTORNEY"
documentKey: "customer/9fdf7ca8-4251-41c8-9116-6d006fa64aea/800123000004054/LIFESAVER_DEATH/LIFESAVER_DEATH_POWER_OF_ATTORNEY_6de88371-c666-4ce5-87f2-8aedf778f4c4.jpg",
...
}

请帮助我理解...非常感谢

5w9g7ksd

5w9g7ksd1#

对象内部只能有唯一键。使用documentCode值作为属性键怎么样?

let newObj = {}
  for ({data} of payload) {
    for (item of data) {
      newObj[item.documentCode] = item.documentKey;
    };
  }
  console.log(newObj);

答复:

{
  "CUSTOMER_CLAIM_FORM": "customer/9fdf7ca8-4251-41c8-9116-6d006fa64aea/800123000004054/LIFESAVER_DEATH/LIFESAVER_DEATH_CUSTOMER_CLAIM_FORM_ca2b0204-b8c4-42f7-b059-580818789a64.jpg",
  "POWER_OF_ATTORNEY": "customer/9fdf7ca8-4251-41c8-9116-6d006fa64aea/800123000004054/LIFESAVER_DEATH/LIFESAVER_DEATH_POWER_OF_ATTORNEY_6de88371-c666-4ce5-87f2-8aedf778f4c4.jpg",
  "POLICE_RECORD": "customer/9fdf7ca8-4251-41c8-9116-6d006fa64aea/800123000004054/LIFESAVER_DEATH/LIFESAVER_DEATH_POLICE_RECORD_15d989de-9cfb-4574-8451-92f445177331.jpg"
}
bzzcjhmw

bzzcjhmw2#

如果我正确地理解了你的问题,那么你所需要做的就是以下面的格式得到答案:

[
   {
     "CUSTOMER_CLAIM_FORM": "SOMETHING",
     "POWER_OF_ATTORNEY": "SOMETHING",
     "POLICE_RECORD": "SOMETHING"
   },
   ...
]

该数组已用作对象数据的父 Package 器,因为您的有效负载是一个数组,并且它将具有更多包含数据的对象。

解决方案

let finalResponse = [];
payload.map((payloadItem) => {
  let myObject = {}
  payloadItem.data.map(data => {
    myObject[data.documentCode] = data.documentKey;
    
    return null;
  });

  finalResponse.push(myObject);

  return null;
});

// No compulsion of using JSON.stringify here
console.log(JSON.stringify(finalResponse));

答复

[
  {
    "CUSTOMER_CLAIM_FORM":"customer/9fdf7ca8-4251-41c8-9116-6d006fa64aea/800123000004054/LIFESAVER_DEATH/LIFESAVER_DEATH_CUSTOMER_CLAIM_FORM_ca2b0204-b8c4-42f7-b059-580818789a64.jpg",
    "POWER_OF_ATTORNEY":"customer/9fdf7ca8-4251-41c8-9116-6d006fa64aea/800123000004054/LIFESAVER_DEATH/LIFESAVER_DEATH_POWER_OF_ATTORNEY_6de88371-c666-4ce5-87f2-8aedf778f4c4.jpg",
    "POLICE_RECORD":"customer/9fdf7ca8-4251-41c8-9116-6d006fa64aea/800123000004054/LIFESAVER_DEATH/LIFESAVER_DEATH_POLICE_RECORD_15d989de-9cfb-4574-8451-92f445177331.jpg"
  }
]

随着您的响应增长,它会不断添加更多的数据,您将看到数据变化时的变化。

相关问题