如何从点差运算符中删除属性? [英] 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',
}
}
transformResponse
transformResponse
[{
drugName: 'HYDROCODONE-HOMATROPINE MBR',
drugStrength: '5MG-1.5MG',
drugForm: 'TABLET',
brand: false
}]
推荐答案
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)
此外,当您在{}
内展开数组时,会得到一个对象,该对象的数组索引为键,而数组的值为value.这就是为什么在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屋!
查看全文