我有以下变量:
filter = {
"country": 1,
"Age Group": {},
"Gender": {},
"NEWSEC": {},
"Telco_Segment": {}
};
和功能:
function facetBuilder(key, val)
{
if(key == 'country')
{
filter.country = val;
}
else
{
if(typeof filter[key][val] !== "undefined" )
{
delete filter[key][val]; //I'm assuming you want to remove it
}
else
{
filter[key][val] = true;
}
}
console.log(filter);
}
生成的对象最终看起来像这样:
filter = {
"country": 1,
"Age Group": {
4: true,
3: true,
2: true
},
"Gender": {
1: true
},
"NEWSEC": {
3: true,
2: true
},
"Telco_Segment": {}
};
但我真正想要的是这样的:
filter = {
"country": 1,
"Age Group": [4,3,2],
"Gender": [1],
"NEWSEC": [3,2],
"Telco_Segment": []
};
而且我不确定如何处理它。