remove $$hashKey from array

后端 未结 2 1280
终归单人心
终归单人心 2020-12-12 18:04
$scope.appdata = [{name: \'\', position: \'\', email: \'\'}];

This is the array which I created in angular controller.

Then I inserted some

相关标签:
2条回答
  • 2020-12-12 18:55

    Based on your comment that you need to insert the array into the database, I'm going to assume that you are converting it a JSON string and then saving it to the DB. If that's incorrect, let me know and I'll see if I can revise this answer.

    You have two options for modifying your array when converting it to JSON. The first is angular.toJson, which is a convenience method that automatically strips out any property names with a leading $$ prior to serializing the array (or object). You would use it like this:

    var json = angular.toJson( $scope.appdata );
    

    The other option, which you should use if you need more fine grained control, is the replacer argument to the built-in JSON.stringify function. The replacer function allows you to filter or alter properties before they get serialized to JSON. You'd use it like this to strip $$hashKey:

    var json = JSON.stringify( $scope.appdata, function( key, value ) {
        if( key === "$$hashKey" ) {
            return undefined;
        }
    
        return value;
    });
    
    0 讨论(0)
  • 2020-12-12 18:57

    Fast solution for lazy people :

    JSON.parse(angular.toJson($scope.appdata))
    
    0 讨论(0)
提交回复
热议问题