I’m losing my mind on this one. My last question answered a syntax issue, but did not address making it function properly, so here goes…
My object:
filter = {
"country": 1,
"Age Group": [],
"Gender": [],
"NEWSEC": [],
"Telco_Segment": []
};
My function:
function buildFacets(key, val)
{
if(key == 'country')
{
filter.country = val;
}
else
{
if(val in filter[key])
{
delete filter[key][val];
}
else
{
filter[key].push(val);
}
}
console.log(filter);
}
My problem is my function is comparing the incoming value to the array indexes, not the array values. For instance, if I call buildFacets(“Age Group”,2) I will get a value in that array of 2. However, if I call it again, it will just add another value of ‘2’ to the array instead of removing the ‘2’ that is already there because it’s comparing to the index, which is 0.
I hope I’m explaining this right. Please let me know if it needs clarification.
1 Answer