1

Is there is a way to transform this JSON Object using Angular? I need to transform the JSON object from this format:

    $scope.TestJson = {
        "filters": [
                {
                    "dataPropertyID": "VoidType",
                    "label": "Homeless"
                },
                {
                    "dataPropertyID": "VoidType",
                    "label": "Mainstream"
                },
                {
                    "dataPropertyID": "PropertyType",
                    "label": "Flat"
                },
                {
                    "dataPropertyID": "PropertyType",
                    "label": "Cottage"
                }
        ]
    }

To this format:

    $scope.NewTestJson = {
        "filters": [
                {
                    "dataPropertyID": "VoidType",
                    "label":[ "Homeless","Mainstream"]
                },
                {
                    "dataPropertyID": "PropertyType",
                    "label":[ "Flat", "Cottage"]
                }
        ]
    }
1
  • Of course you can do it. Go through filters and for each dataPropertyID add its label to an array. Creating a new object by doing this .. Commented Sep 3, 2015 at 10:20

2 Answers 2

1

I think this is more a JavaScript question than anything else. Nonetheless:

$scope.NewTestJson = {
    filters: [];
};

// Do something for all (old) filter items
$scope.TestJson.filters.forEach(function(filter) {
    // Try to get the existing (new) filter
    var newFilter = $scope.NewTestJson.filters.filter(function(newFilter) {
        return newFilter.dataPropertyID === filter.dataPropertyID;
    }).shift();

    // If the new filter does not exist, create it
    if (!newFilter) {
        newFilter = {
            dataPropertyID: filter.dataPropertyID,
            label: []
        };
        $scope.NewTestJson.filters.push(newFilter);
    }

    // Finally, add the old filter label to the new filter
    newFilter.label.push(filter.label);
});
Sign up to request clarification or add additional context in comments.

Comments

0
json = {
        "filters": [
                {
                    "dataPropertyID": "VoidType",
                    "label": "Homeless"
                },
                {
                    "dataPropertyID": "VoidType",
                    "label": "Mainstream"
                },
                {
                    "dataPropertyID": "PropertyType",
                    "label": "Flat"
                },
                {
                    "dataPropertyID": "PropertyType",
                    "label": "Cottage"
                }
        ]
    };

newJson = new Object();
newJson.filters = new Array();


for (var element in json.filters) {
    var check = 0;
    for (var element2 in newJson.filters) {
        if (json.filters[element].dataPropertyID === newJson.filters[element2].dataPropertyID) {
            newJson.filters[element2].label.push(json.filters[element].label);
            check = 1;
        }
    }
    if (check == 0) {
        var Obj = new Object();
        Obj.dataPropertyID = json.filters[element].dataPropertyID;
        Obj.label = new Array();
        Obj.label.push(json.filters[element].label);
        newJson.filters.push(Obj);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.