jQuery 添加到我的 JSON.stringify 结果中的 $$hashKey 是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18826320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the $$hashKey added to my JSON.stringify result
提问by jonnie
I have tried looking on the Mozilla JSON stringifypage of their docs as well as here on SO and Google but found no explanation. I have used JSOn stringify many time but never come across this result
我曾尝试查看他们文档的Mozilla JSON stringify页面以及 SO 和 Google 上的此处,但没有找到任何解释。我已经多次使用 JSOn stringify 但从未遇到过这个结果
I have an array of JSON objects
我有一组 JSON 对象
[
{
"param_2": "Description 1",
"param_0": "Name 1",
"param_1": "VERSION 1"
},
{
"param_2": "Description 2",
"param_0": "Name 2",
"param_1": "VERSION 2"
},
{
"param_2": "Description 3",
"param_0": "Name 3",
"param_1": "VERSION 3"
}
]
attached to my $scope
and in order to POST
them as one paramater I used the JSON.stringify() method and I get the following:
附加到我的$scope
,为了将POST
它们作为一个参数,我使用了 JSON.stringify() 方法,我得到以下信息:
[
{
"param_2": "Description 1",
"param_0": "Name 1",
"param_1": "VERSION 1",
"$$hashKey": "005"
},
{
"param_2": "Description 2",
"param_0": "Name 2",
"param_1": "VERSION 2",
"$$hashKey": "006"
},
{
"param_2": "Description 3",
"param_0": "Name 3",
"param_1": "VERSION 3",
"$$hashKey": "007"
}
]
I am just curious what exactly is the $$hashkey as I expected something more similar to the following from the stringify method:
我只是好奇 $$hashkey 到底是什么,因为我期望从 stringify 方法得到更类似于以下内容的东西:
[
{
"1":{
"param_2": "Description 1",
"param_0": "Name 1",
"param_1": "VERSION 1"
},
"2":{
"param_2": "Description 2",
"param_0": "Name 2",
"param_1": "VERSION 2"
},
"3":{
"param_2": "Description 3",
"param_0": "Name 3",
"param_1": "VERSION 3"
}
}
]
I am not sure if it is a factor but I am using Angularjs 1.1.5, JQuery 1.8.2 and Spring 3.0.4 and Spring security 3.0.7 on the Server side
我不确定这是否是一个因素,但我正在使用 Angularjs 1.1.5, JQuery 1.8.2 and Spring 3.0.4 and Spring security 3.0.7 on the Server side
It is not causeing me any issues but I would like to know the cause and reason for the $$hashkey
这不会给我带来任何问题,但我想知道原因和原因 $$hashkey
回答by David Boike
Angular adds this to keep track of your changes, so it knows when it needs to update the DOM.
Angular 添加了这个来跟踪你的变化,所以它知道什么时候需要更新 DOM。
If you use angular.toJson(obj)
instead of JSON.stringify(obj)
then Angular will strip out these internal-use values for you.
如果您使用angular.toJson(obj)
而不是,JSON.stringify(obj)
那么 Angular 会为您去除这些内部使用的值。
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
此外,如果您更改重复表达式以使用track by {uniqueProperty}
后缀,则 Angular 根本不需要添加$$hashKey
。例如
<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.
只要永远记住你需要“链接”。表达的一部分 - 我总是倾向于忘记这一点。只是track by href
肯定行不通。
回答by rob2universe
In my use case (feeding the resulting object to X2JS) the recommended approach
在我的用例中(将结果对象提供给 X2JS)推荐的方法
data = angular.toJson(source);
help to remove the $$hashKey
properties, but the result could then no longer be processed by X2JS.
帮助删除$$hashKey
属性,但结果无法再由X2JS处理。
data = angular.copy(source);
removed the $$hashKey
properties as well, but the result remained usable as a parameter for X2JS.
也删除了$$hashKey
属性,但结果仍然可用作 X2JS 的参数。
回答by Thomas Pons
It comes with the ng-repeat directive usually. To do dom manipulation AngularJS flags objects with special id.
它通常带有 ng-repeat 指令。做 dom 操作 AngularJS 标记具有特殊 id 的对象。
This is common with Angular. For example if u get object with ngResource your object will embed all the resource API and you'll see methods like $save, etc. With cookies too AngularJS will add a property __ngDebug.
这在 Angular 中很常见。例如,如果您使用 ngResource 获取对象,您的对象将嵌入所有资源 API,您将看到 $save 等方法。使用 cookie 时,AngularJS 也会添加一个属性 __ngDebug。
回答by Michael Falck Wedelg?rd
If you don't want to add id's to your data, you could track by the index in the array, which will cause the items to be keyed by their position in the array instead of their value.
如果您不想将 id 添加到您的数据中,您可以通过数组中的索引进行跟踪,这将导致项目以它们在数组中的位置而不是它们的值作为键。
Like this:
像这样:
var myArray = [1,1,1,1,1];
<li ng-repeat="item in myArray track by $index">
回答by Ajay Ullal
If you are using Angular 1.3 or above, I recommend that you use "track by" in your ng-repeat. Angular doesn't add a "$$hashKey" property to the objects in your array if you use "track by". You also get performance benefits, if something in your array changes, angular doesn't recreate the entire DOM structure for your ng-repeat, it instead recreates the part of the DOM for the values in your array that have changed.
如果您使用的是 Angular 1.3 或更高版本,我建议您在 ng-repeat 中使用“track by”。如果您使用“track by”,Angular 不会向数组中的对象添加“$$hashKey”属性。您还可以获得性能优势,如果数组中的某些内容发生更改,angular 不会为 ng-repeat 重新创建整个 DOM 结构,而是为数组中已更改的值重新创建 DOM 部分。
回答by Vinay
Update : From Angular v1.5, track by $index
is now the standard syntax instead of using link as it gave me a ng-repeat
dupes error.
更新:从 Angular v1.5 开始,track by$index
现在是标准语法,而不是使用链接,因为它给了我一个ng-repeat
欺骗错误。
I ran into this for a nested ng-repeat
and the below worked.
我遇到了这个嵌套ng-repeat
,下面的工作。
<tbody>
<tr ng-repeat="row in data track by $index">
<td ng-repeat="field in headers track by $index">{{row[field.caption] }}</td>
</tr>
回答by Devner
Here is how you can easily remove the $$hashKey from the object:
以下是从对象中轻松删除 $$hashKey 的方法:
$scope.myNewObject = JSON.parse(angular.toJson($scope.myObject))
$scope.myObject
- Refers to the Object that you want to perform the operation upon i.e. remove the $$hashKey from
$scope.myObject
- 指的是您要对其执行操作的对象,即从中删除 $$hashKey
$scope.myNewObject
- Assign the modified original object to the new object so it can be used as necessary
$scope.myNewObject
- 将修改后的原始对象分配给新对象,以便在需要时使用
回答by alfishan aqeel
https://www.timcosta.io/angular-js-object-comparisons/
https://www.timcosta.io/angular-js-object-comparisons/
Angular is pretty magical the first time people see it. Automatic DOM updates when you update a variable in your JS, and the same variable will update in your JS file when someone updates its value in the DOM. This same functionality works across page elements, and across controllers.
当人们第一次看到 Angular 时,它非常神奇。当您更新 JS 中的变量时,DOM 会自动更新,并且当有人更新 DOM 中的值时,您的 JS 文件中也会更新相同的变量。相同的功能适用于页面元素和控制器。
The key to all of this is the $$hashKey Angular attaches to objects and arrays used in ng-repeats.
所有这一切的关键是 $$hashKey Angular 附加到 ng-repeat 中使用的对象和数组。
This $$hashKey causes a lot of confusion for people who are sending full objects to an API that doesn't strip extra data. The API will return a 400 for all of your requests, but that $$hashKey just wont go away from your objects.
这个 $$hashKey 给那些将完整对象发送到不剥离额外数据的 API 的人造成了很多困惑。API 将为您的所有请求返回 400,但 $$hashKey 不会离开您的对象。
Angular uses the $$hashKey to keep track of which elements in the DOM belong to which item in an array that is being looped through in an ng-repeat. Without the $$hashKey Angular would have no way to apply changes the occur in the JavaScript or DOM to their counterpart, which is one of the main uses for Angular.
Angular 使用 $$hashKey 来跟踪 DOM 中的哪些元素属于在 ng-repeat 中循环的数组中的哪个项目。如果没有 $$hashKey Angular 将无法将 JavaScript 或 DOM 中发生的更改应用到它们的对应项,这是 Angular 的主要用途之一。
Consider this array:
考虑这个数组:
users = [
{
first_name: "Tim"
last_name: "Costa"
email: "[email protected]"
}
]
If we rendered that into a list using ng-repeat="user in users", each object in it would receive a $$hashKey for tracking purposes from Angular. Here are two ways to avoid this $$hashKey.
如果我们使用 ng-repeat="user in users" 将其渲染到列表中,那么其中的每个对象都会收到一个 $$hashKey 用于从 Angular 进行跟踪。这里有两种方法可以避免这个 $$hashKey。