I want to create a dictionary in JavaScript by checking for the objects property.
var item = {};
if(item.hasOwnProperty("xyz")){
//do wat is required
}else{
//add the key property to the item object
}
How to add this "xyz" key property to the object is my question.
Thanks
You just need to use item.xyz='Whatever'
and xyz
will be added to item
var item = {};
if (item.hasOwnProperty('xyz')) {
console.log('item has xyz');
} else {
item.xyz = 'something';
//item["xyz"] = 'something'; You can also use this
}
console.log(item);
DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With