[JS] Remove Object Keys
Output
var thisIsObject= {
'Cow' : 'Moo',
'Cat' : 'Meow',
'Dog' : 'Bark'
};
try {
delete thisIsObject['Cow'];
} catch(e){
thisIsObject.cow = undefined;
}
//test using developer tools F12
console.log(thisIsObject);
Wrapping in function for Easy Use
=> {Cat: "Meow", Dog: "Bark"}
Test Wrapped function
function delkey(obj, key){
try {
delete obj[key];
} catch(e){
obj[key] = undefined;
}
return obj;
}
Output wrapped function
var thisIsObject= {
'Cow' : 'Moo',
'Cat' : 'Meow',
'Dog' : 'Bark'
};
//test using developer tools F12
console.log(delkey(thisIsObject, 'Cow'));
=> {Cat: "Meow", Dog: "Bark"}
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.