hus*_*ain 38 javascript arrays ecmascript-6
我想从响应中删除 drugName 但它没有发生任何想法如何从传播运算符中删除属性?主文件
const transformedResponse = transformResponse(response);
const loggerResponse = {...transformedResponse};
delete loggerResponse[drugName];
console.log("LOGGER>>>>", loggerResponse);
logger().info('Drug Price Response=', { ...loggerResponse, memberId: memberId, pharmacyId: pharmacyId });
Run Code Online (Sandbox Code Playgroud)
\ 数据
LOGGER>>>> {
'0': {
isBrand: false,
drugName: 'test drug',
drugStrength: '5 mg 1 5 mg',
drugForm: 'Tablet',
}
}
Run Code Online (Sandbox Code Playgroud)
变换响应
[{
drugName: 'HYDROCODONE-HOMATROPINE MBR',
drugStrength: '5MG-1.5MG',
drugForm: 'TABLET',
brand: false
}]
Run Code Online (Sandbox Code Playgroud)
adi*_*iga 82
您可以在对象解构中使用Rest 语法来获取除像这样drugName
的rest
变量之外的所有属性:
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)
Run Code Online (Sandbox Code Playgroud)
此外,当您在 中展开数组时{}
,您会得到一个对象,该对象以数组的索引作为键,以数组的值作为值。这就是为什么你会得到一个带有0
as 键的对象loggerResponse
:
const array = [{ id: 1 }, { id: 2 }]
console.log({ ...array })
Run Code Online (Sandbox Code Playgroud)
Dhr*_*_04 31
const loggerResponse = {
"0": {
isBrand: false,
drugName: "test drug",
drugStrength: "5 mg 1 5 mg",
drugForm: "Tablet",
},
};
const { drugName, ...newResponse } = loggerResponse["0"];
console.log(newResponse);
Run Code Online (Sandbox Code Playgroud)
Tha*_*you 16
另一种选择是编写一个通用函数,removeKey
-
const removeKey = (k, { [k]:_, ...o }) =>
o
const values =
[ { a: 1, x: 1 }
, { a: 1, y: 1 }
, { a: 1, z: 1 }
]
console .log (values .map (v => removeKey ("a", v)))
// [ { x: 1 }, { y: 1 }, { z: 1 } ]
Run Code Online (Sandbox Code Playgroud)
如有必要,该功能可以轻松调整以删除多个键 -
const removeKey = (k = "", { [k]:_, ...o } = {}) =>
o
const removeKeys = (keys = [], o = {}) =>
keys .reduce ((r, k) => removeKey (k, r), o)
const values =
[ { a: 1, x: 1 }
, { a: 1, y: 1 }
, { a: 1, z: 1 }
]
console .log (values .map (v => removeKeys (['a', 'z'], v)))
// [ { x: 1 }, { y: 1 }, {} ]
Run Code Online (Sandbox Code Playgroud)
小智 12
另一种单行解决方案是,
return {...(delete obj.del && obj)}
Run Code Online (Sandbox Code Playgroud)
这是我发现的最简洁、最不变的方式。您只需将对象解构为两部分:一部分是您要删除的属性(drugName
在本例中),另一部分是您要保留的对象的其余部分(drugWithoutName
在本例中)。
完成此操作后,您可以放弃已删除的属性,放弃原始对象,并使用drugWithoutName
具有所有剩余字段的新对象(在本例中)。
语法并不明显,但一旦你看到它就很有意义:
const response = [{
drugName: 'HYDROCODONE-HOMATROPINE MBR',
drugStrength: '5MG-1.5MG',
drugForm: 'TABLET',
brand: false
}]
console.log("response", response)
const removeNamesFromResponse = (response) => {
return response.map(drug => {
const { drugName, ...drugWithoutName } = drug
return drugWithoutName
})
}
const responseWithoutNames = removeNamesFromResponse(response)
console.log("responseWithoutNames", responseWithoutNames)
Run Code Online (Sandbox Code Playgroud)
这些文章进一步解释了这个概念:
https://codeburst.io/use-es2015-object-rest-operator-to-omit-properties-38a3ecffe90
https://github.com/airbnb/javascript/blob/master/README.md#objects--rest-spread
小智 8
const myObject = {
a: 1,
b: 2,
c: 3
};
const {
b,
...noA
} = myObject;
console.log(noA); // => { a: 1, c: 3 }
Run Code Online (Sandbox Code Playgroud)