简体   繁体   中英

How to remove empty key from object in angularjs?

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM