如果对象存在,则jquery对象数组附加值

时间:2022-09-25 09:10:28

I have an array of objects in javascript. What I want is to check one property of the objects in the array and if that property is present then another property should be appended, else create new array of objects. The array of objects is like this:

我在javascript中有一个对象数组。我想要的是检查数组中对象的一个​​属性,如果该属性存在,则应附加另一个属性,否则创建新的对象数组。对象数组是这样的:

[{
    filtercolumnname: "countrycode",
    filterValue: "US,UK"
}]

I have to check if filtercolumnname is present in that array then add another value to existing values like US,UK,IE,IN else create new array. So the array should be:

我必须检查该数组中是否存在filtercolumnname,然后将另一个值添加到现有值,如US,UK,IE,IN,否则创建新数组。所以数组应该是:

[{
    filtercolumnname: "countrycode",
    filterValue: "US,UK,IE,IN"
}]

I tried this but not able to think how to active this

我试过这个但却无法想到如何激活它

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

1 个解决方案

#1


0  

this solved my issue.

这解决了我的问题。

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

#1


0  

this solved my issue.

这解决了我的问题。

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