0

i have a list in json and ai need it with unique values

[
    {
        "equipmentType": "5 MEG DOCSIS",
        "status": "On Truck ",
        "count": 2
    },
    {
        "equipmentType": "5 MEG DOCSIS",
        "status": "Return Faulty",
        "count": 3
    } ]

I need like that with unique equipmentType :

[
        {
            "equipmentType": "5 MEG DOCSIS",
            "On_TruckCount": 2,
            "Return_Faulty": 3

        }
]

Help me to find out solution of that problem Thanks in advance..

EDIT

I am trying with that but its wrong, here obj is json obj.

$scope.equipmentDashboard = obj;
$scope.oldEquip="";
$scope.listtest='';
$scope.test = [];
angular.forEach(obj, function(value, key) {
    if(value.equipmentType == $scope.oldEquip.equipmentType){
        if(value.status=="On Truck ")
            $scope.listtest.onTruck = value.count;
        else
            $scope.listtest.returnFaulty = value.count;
    } else {
        $scope.test.push($scope.listtest);
        $scope.oldEquip = value;
    }           
});
3
  • Please show what you have tried to accomplish this mapping. So far all you have is some data and a request for someone to do your work for you. You haven't shown any attempt to solve this yourself Commented Jul 19, 2015 at 12:39
  • This tool helped me once, when I have to map something... use underscorejs.org Commented Jul 19, 2015 at 12:43
  • @charlietfl i have added my code, but its wrong, dnt know right logic abot it Commented Jul 19, 2015 at 12:46

1 Answer 1

1

For something like this I would use native javascript to create a temporary object using equipmentType values as property keys since that is what is common between objects in array.

Since this is all preprocessing we don't need to worry about angular scope until the end. Ideally you would do this mapping in a service and let service return results

var tmp = {}
data.forEach(function (row) {
    if (!tmp[row.equipmentType]) {
        tmp[row.equipmentType] = {
            equpmentType: row.equipmentType
        };
    }
    if (!tmp[row.equipmentType][row.status] ) {
        tmp[row.equipmentType][row.status] = 0;
    }
    tmp[row.equipmentType][row.status] += row.count;

});

Temp object will produce:

{
    "5 MEG DOCSIS": {
        "equpmentType": "5 MEG DOCSIS",
        "On Truck ": 2,
        "Return Faulty": 3
    }
}

Then iterate over temp object to push each child object into array that will be used in $scope

var out = [];
for (var key in tmp) {
    if (tmp.hasOwnProperty(key)) {
        out.push(tmp[key]);
    }
}
$scope.equipmentList = out;

DEMO

Sign up to request clarification or add additional context in comments.

3 Comments

Wao that is awesome. thanks alot . but i am getting an error assignment to undeclared variable key on this line for (key in tmp) {
make it for (var key in tmp)...I was being lazy and wasn't using "use strict" in fiddle
@ charlietfl Yes thanks Its working. you save my day :)

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.