35
$scope.appdata = [{name: '', position: '', email: ''}];

This is the array which I created in angular controller.

Then I inserted some values in to array by using push method:

$scope.appdata.push({name: 'jenson raby', position: '2', email: 'jensonraby@gmail.com'});

This is the array values after insertion:

$$hashKey:"00F",name:"jenson raby",position:"2",email:"jensonraby@gmail.com",

Here an extra $$hashKey has been added to the array after insertion, but I need to remove this from array.

Jenson Raby
  • 769
  • 2
  • 6
  • 26
  • Are you using any AngularJS service to handle Rest API ike Restangular? – forgottofly Sep 02 '15 at 04:12
  • I'm not sure why you want to remove the `$$hashKey`, but if your question really is "What is the `$$hashKey`?", [this may help](https://stackoverflow.com/questions/18826320/what-is-the-hashkey-added-to-my-json-stringify-result). – Ken Y-N Sep 02 '15 at 04:22
  • 1
    i need to insert the above array to database, that's why i need to remove – Jenson Raby Sep 02 '15 at 04:32

3 Answers3

59

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;
});
Sean Walsh
  • 8,266
  • 3
  • 30
  • 38
  • @Sean, Hi Sean, I have one clarification that Is there any possibility to $$hashkey goes duplication? I have used JSOn.stringify(Obj) and I am able to see duplicate hashkey in DB. – Md Aslam Jan 08 '19 at 10:04
  • @MdAslam I don't believe there's any guarantee of uniqueness with `$$hashKey` - it's just an ID Angular uses to keep track of changes. You should not use it as a unique identifier. – Sean Walsh Jan 10 '19 at 16:18
  • Great solution, it's perfect for my use case. Do you know if the replacer function mutates the original object ($scope.appdata in your example)? – Adam Specker Feb 19 '21 at 01:23
  • @AdamSpecker It does not mutate the original object, just changes the way in which it gets stringified. – Sean Walsh Feb 27 '21 at 01:20
30

Fast solution for lazy people :

JSON.parse(angular.toJson($scope.appdata))
Yahia Mgarrech
  • 682
  • 9
  • 14
  • 2
    I am puzzled, I am doing exactly this in google chrome and angularjs, tried also all other suggestions and $$hashKey still appears. When is it inserted there? Any hints? – VinGarcia Sep 27 '17 at 17:13
  • I still haven't understood why it is happening but when I did this: `$scope.steps = JSON.parse(angular.toJson($scope.steps)); console.log($scope.steps);` the `$$hashKey` was there, if I saved it in a different variable which was not in an ng-repeat it worked. – VinGarcia Sep 27 '17 at 17:38
  • 3
    I think Angular add $$hashkey to keep track of scope objects as an ID. This is called Mutability in Javascript. – Yahia Mgarrech Sep 28 '17 at 18:54
  • VinGarcia, thank you! Thanks to your lead I discovered that my ng-change was set to the same thing as my scope. Namely ng-change="categories" and then $scope.categories = ... As soon as I changed ng-change to ng-change="category" the problem dissappeared! – Lukas Jul 11 '18 at 23:21
0

The code behind the angular.toJson method is as follows:

function toJson(obj, pretty) {
  if (isUndefined(obj)) return undefined;
  if (!isNumber(pretty)) {
    pretty = pretty ? 2 : null;
  }
  return JSON.stringify(obj, toJsonReplacer, pretty);
}

When the JSON.stringify method is called it will strip out anything that starts with $

function toJsonReplacer(key, value) {
  var val = value;

  if (typeof key === 'string' && key.charAt(0) === '$' && key.charAt(1) === '$') {
    val = undefined;
  } else if (isWindow(value)) {
    val = '$WINDOW';
  } else if (value &&  window.document === value) {
    val = '$DOCUMENT';
  } else if (isScope(value)) {
    val = '$SCOPE';
  }

  return val;
}

So to build on Sean's asnwer, you could override the JSON.stringify method to do exactly what you need it to do. To see more from the documentation of angular visit: https://github.com/angular/angular.js/pull/14221

Max Pringle
  • 621
  • 6
  • 18