【问题标题】:jquery array of objects append value if object is present如果对象存在,jquery对象数组附加值
【发布时间】:2015-09-23 09:58:49
【问题描述】:

我在 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 
            });
        }
    })
}

【问题讨论】:

    标签: jquery arrays asp.net-mvc


    【解决方案1】:

    这解决了我的问题。

     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);
        }
    

    【讨论】:

      猜你喜欢
      • 2014-05-15
      • 1970-01-01
      • 2022-10-04
      • 2019-02-18
      • 2020-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多