我在javascript中有一个对象数组。我想要的是检查数组中对象的一个属性,如果该属性存在,则应附加另一个属性,否则创建新的对象数组。对象数组如下:
[{
filtercolumnname: "countrycode",
filterValue: "US,UK"
}]
我必须检查该数组中是否存在filtercolumnname
,然后将另一个值添加到现有值,如US,UK,IE,IN
,否则创建新数组。所以数组应该是:
[{
filtercolumnname: "countrycode",
filterValue: "US,UK,IE,IN"
}]
我试过了,但没想到如何激活这个
Var SelectedAttribute = "countrycode";
SelectedPoints = JSON.parse(ChartProperties[this._id]);
if (!SelectedPoints.hasOwnProperty("filterArray")) {
(SelectedPoints.filterArray).each(function() {
if (SelectedPoints.filterArray.filterColumnName.match(SelectedAttribute)) {
SelectedPoints.filterArray.filterValue += series.points[sender.data.region.Region.PointIndex].x;
} else {
jsondata.filterArray.push({
"filterValue": series.points[sender.data.region.Region.PointIndex].x,
"filterColumnName": SelectedAttribute
});
}
})
}
答案 0 :(得分:0)
这解决了我的问题。
SelectedPoints = JSON.parse(ChartProperties[this._id]);
Var SelectedAttribute = "countrycode";
if (SelectedPoints.hasOwnProperty("filterArray"))
{
for (var i = 0; i < SelectedPoints.filterArray.length; i++) {
if (SelectedPoints.filterArray[i].filterColumnName.match(SelectedAttribute)) {
SelectedPoints.filterArray[i].filterValue += "," + series.points[sender.data.region.Region.PointIndex].x;
} else {
jsondata.filterArray.push({ "filterValue": series.points[sender.data.region.Region.PointIndex].x, "filterColumnName": SelectedAttribute });
}
}
} else {
SelectedPoints["filterArray"] = [];
SelectedPoints.filterArray.push({ "filterValue": series.points[sender.data.region.Region.PointIndex].x, "filterColumnName": SelectedAttribute });
ChartProperties[this._id]= JSON.stringify(SelectedPoints);
}