gpt4 book ai didi

javascript - 如何使用 JS spread rest destruction 删除嵌套属性?

转载 作者:行者123 更新时间:2023-12-05 03:01:04 26 4
gpt4 key购买 nike

尝试删除多个属性 usign spread operator rest destruction,所以 drugName 被删除但我有嵌套对象 mailPrice.copayEmployer 没有被破坏不知道什么是实现错误?

主要.js

const transformedResponse = transformResponse(response);
const loggerResponse = transformedResponse.map(
({drugName, mailPrice.copayEmployer, ...rest}) => rest
);

转换响应

[{
"isBrand": true,
"drugName": "Lipitor",
"drugStrength": "80 mg",
"drugForm": "Tablet",
"mailPrice": {
"copayEmployer": 0
}
}, {
"isBrand": true,
"drugName": "Metformin",
"drugStrength": "500 mg",
"drugForm": "Tablet",
"mailPrice": {
"copayEmployer": 50
}
}

]

最佳答案

我不认为你可以用一个变量来做到这一点。您需要在 mailPrice 对象中嵌套一个 ...rest 变量,然后您必须重建该对象。

const transformedResponse = [{
"isBrand": true,
"drugName": "Lipitor",
"drugStrength": "80 mg",
"drugForm": "Tablet",
"mailPrice": {
"copayEmployer": 0,
}
}, {
"isBrand": true,
"drugName": "Metformin",
"drugStrength": "500 mg",
"drugForm": "Tablet",
"mailPrice": {
"copayEmployer": 50
}
}];

const loggerResponse = transformedResponse.map(
({
drugName,
mailPrice: { copayEmployer, ...mailPriceRest},
...rest
}) => ({mailPrice: {...mailPriceRest}, ...rest})
);

console.log(loggerResponse);

关于javascript - 如何使用 JS spread rest destruction 删除嵌套属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56157765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com