xxxxxxxxxx
var person = {"first_name": "Billy","last_name": "Johnson"};
delete person.last_name; //delete last_name property
xxxxxxxxxx
var person = {
name: "Harry",
age: 16,
gender: "Male"
};
// Deleting a property completely
delete person.age;
alert(person.age); // Outputs: undefined
console.log(person); // Prints: {name: "Harry", gender: "Male"}
xxxxxxxxxx
var ourDog = {
"name": "Camper",
"legs": 4,
"tails": 1,
"friends": ["everything!"],
"bark": "bow-wow"
};
delete ourDog.bark;
You can remove a property from an object using destructuring in combination with the ... rest operator. Destructuring splits an object into its individual keys and you can remove those that you don’t want in the new one.
Here’s an example removing the group property from a user object:
xxxxxxxxxx
const user = { id: 1, name: 'Marcus', group: 'admin' }
const { ['group']: group, userWithoutGroup } = user
console.log(userWithoutGroup) // { id: 1, name: 'Marcus' }
The code snippet destructures the user into a group property and everything else called userWithoutGroup
source : https://futurestud.io/tutorials/how-to-delete-a-key-from-an-object-in-javascript-or-node-js
xxxxxxxxxx
let myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};
const {regex, newObj} = myObject;
console.log(newObj); // has no 'regex' key
console.log(myObject); // remains unchanged
xxxxxxxxxx
let reqQuery= { select: 'name,description,createdAt', sort: 'name' }
const removeFields = ['select']
//Loop over removeFields and delete them from reqQuery
removeFields.forEach(param=> delete reqQuery[param])
console.log(reqQuery) //{sort:'name'}
xxxxxxxxxx
//Easy way around by using rest parameters
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
var myObject = {
"ircEvent": "PRIVMSG",
"method": "newURI",
"regex": "^http://.*"
};
delete myObject.regex;
console.log(myObject);