xxxxxxxxxx
const response = [{
drugName: 'HYDROCODONE-HOMATROPINE MBR',
drugStrength: '5MG-1.5MG',
drugForm: 'TABLET',
brand: false
},
{
drugName: 'HYDROCODONE ABC',
drugStrength: '10MG',
drugForm: 'SYRUP',
brand: true
}]
const output = response.map(({ drugName, rest }) => rest)
/* output = [{
drugStrength: '5MG-1.5MG',
drugForm: 'TABLET',
brand: false
},
{
drugStrength: '10MG',
drugForm: 'SYRUP',
brand: true
}]
*/
xxxxxxxxxx
let user = {
name: 'Calvin',
age: 200,
country: 'Spain',
food: 'Pizza'
}
const {name, restOfUser} = user
console.log(restOfUser)
console.log(name)
// { age: 200, country: 'Spain', food: 'Pizza' }
// Calvin
xxxxxxxxxx
let obj = {foo: 1, bar: 2, baz: 3}
function removeProperty(obj, propertyName) {
let { [propertyName]: _, result } = obj
return result
}
console.log(removeProperty(obj, 'foo'));
xxxxxxxxxx
+-----------------------------------+
| Browser | delete | destructure |
+---------+-----------+-------------+
| Chrome | 3,229,791 | 1,993,256 |
| Safari | 1,186,679 | 1,872,396 |
+---------+-----------+-------------+
xxxxxxxxxx
let obj = {foo: 1, bar: 2, baz: 3}
function removeProperty(obj, propertyName) {
let newObj = {obj};
delete newObj[propertyName];
return newObj;
}
console.log(removeProperty(obj, 'foo'));