[JS] Remove Object Keys

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);
Output
=> {Cat: "Meow", Dog: "Bark"}
Wrapping in function for Easy Use
function delkey(obj, key){
  try {
    delete obj[key];
  } catch(e){
    obj[key] = undefined;
  }
  return obj;
}
Test Wrapped function
var thisIsObject= {
    'Cow' : 'Moo',
    'Cat' : 'Meow',
    'Dog' : 'Bark'
};

//test using developer tools F12
console.log(delkey(thisIsObject, 'Cow'));
Output wrapped function
=> {Cat: "Meow", Dog: "Bark"}

Subscribe to receive free email updates:

0 Response to "[JS] Remove Object Keys"

Post a Comment

Bila Ada posting yang kurang, atau error atau yang lainnya, silahkan tinggalkan komentar agar artikel/post di perbaiki.
Jangan Lupa Cek Box "Notify Me" agar tahu komentar kamu dibalas oleh saya.

If there are any posts that are missing, or error or anything else, please leave a comment for the article / post to be fixed.
Do not Forget Check Box "Notify Me" to know our comments replied by me.