使用角度访问响应数据。带有数值命名字段的js

时间:2022-06-20 12:30:45

My response object has a field called "50", so right now I'm trying to access and save that data doing something like this:

我的响应对象有一个名为“50”的字段,所以现在我尝试访问并保存数据,做如下操作:

var thing = $scope.data.array[0].50;

var的事= $ scope.data.array[0]50;

However I'm getting an error on the console when I simply reload the page with the function not even running. When I get rid of the 50, everything is fine. There is indeed a field called "50" inside the $scope.data.array[0] and I do not have access to change the response. Is there something wrong with this because the field is called "50" and maybe JS is interrupting that as a number instead??

但是,当我简单地用未运行的函数重新加载页面时,控制台出现了错误。当我去掉这50个的时候,一切都很好。在$scope.data中确实有一个名为“50”的字段。数组[0]和我没有权限更改响应。这有什么问题吗?因为这个字段被称为“50”,也许JS把它作为一个数字打断了?

Also when I changed "50" to something random like "af", then I get no errors on refresh.

同样,当我将“50”改成“af”时,刷新时不会出现错误。

this doesn't work var thing = $scope.data.array[0].50;

它不能工作var = $scope.data.array[0]。50;

this works var thing = $scope.data.array[0].af;

这个值等于$scope.data.array[0].af;

1 个解决方案

#1


3  

The following should work if your first element of the array has a property called "50".

如果数组的第一个元素具有一个名为“50”的属性,则应执行以下操作。

var thing = $scope.data.array[0]["50"];

var的事= $ scope.data.array[0](“50”);

Property accessors provide access to an object's properties by using the dot notation or the bracket notation.

属性访问器通过使用点符号或方括号符号提供对对象属性的访问。

Syntax
object.property
object["property"]

JavaScript objects are also associative arrays (hashes). Using these you can associate a key string with a value string as shown in the example above.

JavaScript对象也是关联数组(散列)。使用这些方法,您可以将一个键字符串与一个值字符串关联起来,如上例所示。

The reason as to why you don't get an error when accessing $scope.data.array[0].af; is because "af" is valid identifier for a property. Dot notation only works with property names that are valid identifiers. An identifier must start with a letter, $, _ or unicode escape sequence.

为什么在访问$scope.data.array[0].af时不会出现错误?因为“af”是属性的有效标识符。点表示法只适用于有效标识符的属性名。标识符必须以字母、$、_或unicode转义序列开头。

For all other property names, you must use bracket notation.

对于所有其他属性名,必须使用方括号表示法。

#1


3  

The following should work if your first element of the array has a property called "50".

如果数组的第一个元素具有一个名为“50”的属性,则应执行以下操作。

var thing = $scope.data.array[0]["50"];

var的事= $ scope.data.array[0](“50”);

Property accessors provide access to an object's properties by using the dot notation or the bracket notation.

属性访问器通过使用点符号或方括号符号提供对对象属性的访问。

Syntax
object.property
object["property"]

JavaScript objects are also associative arrays (hashes). Using these you can associate a key string with a value string as shown in the example above.

JavaScript对象也是关联数组(散列)。使用这些方法,您可以将一个键字符串与一个值字符串关联起来,如上例所示。

The reason as to why you don't get an error when accessing $scope.data.array[0].af; is because "af" is valid identifier for a property. Dot notation only works with property names that are valid identifiers. An identifier must start with a letter, $, _ or unicode escape sequence.

为什么在访问$scope.data.array[0].af时不会出现错误?因为“af”是属性的有效标识符。点表示法只适用于有效标识符的属性名。标识符必须以字母、$、_或unicode转义序列开头。

For all other property names, you must use bracket notation.

对于所有其他属性名,必须使用方括号表示法。