I have an object menuNamesthat should contain a list of menu items. If it menuNamesalready has slug, add a value, if it does not contain slug, set the value to 1. I do this to track unique names. I want to get something like:
menuNames: {
home: 1,
products: 10,
contact: 1
}
this does not work (this will be contained in a loop going through each pool):
menuNames[slug] = (menuNames.hasOwnProperty(slug) ? menuNames[slug]++ : 1);
//this sets every value to 1
but this works (this will be contained in a loop going through each pool):
if(menuNames.hasOwnProperty(slug)) {
menuNames[slug]++;
} else {
menuNames[slug] = 1;
}
source
share