What is the $$hashKey added to my JSON.stringify result
Angular adds this to keep track of your changes, so it knows when it needs to update the DOM.
If you use angular.toJson(obj)
instead of JSON.stringify(obj)
then Angular will strip out these internal-use values for you.
Also, if you change your repeat expression to use the track by {uniqueProperty}
suffix, Angular won't have to add $$hashKey
at all. For example
<ul>
<li ng-repeat="link in navLinks track by link.href">
<a ng-href="link.href">{{link.title}}</a>
</li>
</ul>
Just always remember you need the "link." part of the expression - I always tend to forget that. Just track by href
will surely not work.
JSON.Stringify | Output the result on the screen with quote tags ''
There is no sense to use JSON.stringify(), if data is not object, try to change
this JSON.stringify(lconfigInfo.sql[configInfoKey])
to lconfigInfo.sql[configInfoKey]
.
Also there is condition where you check does the lconfigInfo.sql[configInfoKey]
is string and trying to transform it again to string.
remove $$hashKey from array
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;
});
In ngRepeat automatically including $$hashKey
AngularJS adds a $$hashKey
attribute to your objects to keep track of your changes if no track by
isset inside your ng-repeat
directive. Simply try to use ng-repeat="item in data track by item.id"
and you will be fine. In that way AngularJS uses this "unique" value for its internal tracking.
View
<div ng-controller="MyCtrl">
<div ng-repeat="item in data track by item.id">
{{ item | json }}
</div>
<br />
<div ng-repeat="item in hashData">
{{ item | json }}
</div>
<br />
<br />
<button ng-click="track()">
Click me to log
</button>
</div>
AngularJS application
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl', function($scope) {
$scope.data = [{
"id": "1",
"name": "product 1",
"image": null
}, {
"id": "2",
"name": "product 2",
"image": null
}, {
"id": "3",
"name": "product 3",
"image": null
}];
$scope.hashData = [{
"id": "1",
"name": "product 1",
"image": null
}, {
"id": "2",
"name": "product 2",
"image": null
}, {
"id": "3",
"name": "product 3",
"image": null
}];
$scope.track = function () {
console.log($scope.data);
console.log($scope.hashData);
}
});
> demo fiddle
How to make Json.stringify ignore certain class memebers?
haxe.Json
has no mechanism to exclude fields, so I would recommend using a third-party library such as json2object that does. Here you can simply annotate fields that should be ignored with @:jignored
:
@:jignored
public var levelName:String;
var data = new GameData(100, 10, "Level 1");
var json = new json2object.JsonWriter<GameData>().write(data);
trace(json); // {"playerHealth": 100,"playerScore": 10}
There are some possible workarounds that don't involve adding a library to your project, but they don't seem very nice:
Don't serialize the object directly, but a structure that only includes the desired fields:
var data = new GameData(100, 10, "Level 1");
var json = Json.stringify({
playerHealth: data.playerHealth,
playerScore: data.playerScore
});
trace(json); // {"playerHealth":100,"playerScore":10}Remove the unwanted fields after serialization - this seems rather hacky as it involves a lot of unnecessary overhead due to an additional
Json.parse()
andJson.stringify()
call:var json = Json.stringify(new GameData(100, 10, "Level 1"));
var data:haxe.DynamicAccess<String> = Json.parse(json);
data.remove("levelName");
json = Json.stringify(data);
trace(json); // {"playerHealth":100,"playerScore":10}
Related Topics
Passing Functions to Settimeout in a Loop: Always the Last Value
Escape String - Output Rails String in JavaScript
Elliptic Curve Cryptography with Sjcl in Js and Openssl in Ruby
How to Tell If a <Script> Tag Failed to Load
What Is the Lifecycle of an Angularjs Controller
Can Mustache Templates Do Template Extension
Calling Node.Js Script from Rails App Using Execjs
Inline Ruby in :JavaScript Haml Tag
How to Get the Selected Radio Button Value Using Js
Losing "This" Context in JavaScript When Passing Around Members
Splitting a Js Array into N Arrays
How to Pre-Populate a Jquery Datepicker Textbox with Today's Date
Preloader Wont Ignore Websocket - Pace Js
Extending an Object in JavaScript