2

I have an JSONarray which looks like this,

        {
        "TotalMemory": [{
            "key": "TotalMemory",
            "values": [
                [118, 10]
            ]
        }],
        "Freememory": [{
            "key": "Freememory",
            "values": [
                [121, 10]
            ]
        }],
        "BufferSize": [{
            "key": "BufferSize",
            "values": [
                [123, 10]
            ]
        }],
        "TotalSwapMemory": [{
            "key": "TotalSwapMemory",
            "values": [
                [125, 10]
            ]
        }],
        "UsedSwapMemory": [{
            "key": "UsedSwapMemory",
            "values": [
                [127, 10]
            ]
        }],
        "FeeSwapMemory": [{
            "key": "FeeSwapMemory",
            "values": [
                [129, 10]
            ]
        }],
        "": [{
            "key": "",
            "values": []
        }]
    }

i need to remove the last item which is empty there. I have followed Remove empty , but this does not work.

$scope.displayData  = $scope.displayData.filter(function() { return true; });
console.log(angular.toJson($scope.displayData));
Community
  • 1
  • 1
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396

2 Answers2

8

You can use Object.keys and delete, like so

Object.keys($scope.displayData).forEach(function(key) {
    if (!key) {
        delete $scope.displayData[key];
    }
}); 

$scope.displayData is Object, .filter is Array method, in Object there is no .filter method

Oleksandr T.
  • 76,493
  • 17
  • 173
  • 144
2

Your structure is not an Array, it is an Object with keys (properties) and values.

To remove the empty item, you can simply use delete:

delete $scope.displayData[""];

This will not error if $scope.displayData[""] does not exist.

Rhumborl
  • 16,349
  • 4
  • 39
  • 45