如何从传播运算符中删除属性? [英] How to delete property from spread operator?
本文介绍了如何从传播运算符中删除属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从响应中删除 drugName 但它没有发生任何想法如何从扩展运算符中删除属性?main.js
I want to delete drugName from the response but it is not happening any idea how to delete property from spread operator ? main.js
const transformedResponse = transformResponse(response);
const loggerResponse = {...transformedResponse};
delete loggerResponse[drugName];
console.log("LOGGER>>>>", loggerResponse);
logger().info('Drug Price Response=', { ...loggerResponse, memberId: memberId, pharmacyId: pharmacyId });
\数据
LOGGER>>>> {
'0': {
isBrand: false,
drugName: 'test drug',
drugStrength: '5 mg 1 5 mg',
drugForm: 'Tablet',
}
}
转换响应
[{
drugName: 'HYDROCODONE-HOMATROPINE MBR',
drugStrength: '5MG-1.5MG',
drugForm: 'TABLET',
brand: false
}]
推荐答案
你可以使用 对象解构中的Rest 语法 将除drugName
之外的所有属性放到一个rest
变量中,如下所示:
You could use Rest syntax in Object Destructuring to get all the properties except drugName
to a rest
variable like this:
const transformedResponse = [{
drugName: 'HYDROCODONE-HOMATROPINE MBR',
drugStrength: '5MG-1.5MG',
drugForm: 'TABLET',
brand: false
},
{
drugName: 'HYDROCODONE ABC',
drugStrength: '10MG',
drugForm: 'SYRUP',
brand: true
}]
const output = transformedResponse.map(({ drugName, ...rest }) => rest)
console.log(output)
此外,当您在 {}
中展开数组时,您会得到一个对象,该对象以数组的索引为键,以数组的值为值.这就是为什么你在 loggerResponse
中得到一个以 0
作为键的对象:
Also, when you spread an array inside {}
, you get an object with indices of the array as key and the values of array as value. This is why you get an object with 0
as key in loggerResponse
:
const array = [{ id: 1 }, { id: 2 }]
console.log({ ...array })
这篇关于如何从传播运算符中删除属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文