It seems that when I use a numeric type as a key name in an object, it always gets converted to a string. Is there anyway to actually get it to store as a numeric? The normal typecasting does not seem to work.
似乎当我在对象中使用数字类型作为键名时,它总是被转换为字符串。反正有没有把它作为数字存储?正常的类型转换似乎不起作用。
Example:
例:
var userId = 1;
console.log( typeof userId ); // number
myObject[userId] = 'a value';
console.dir(myObject);
Dir Output:
目录输出:
{
'1': 'a value'
}
What I want is this:
我想要的是这个:
{
1: 'a value'
}
Advice?
建议吗?
Thanks
谢谢
5 个解决方案
#1
56
No, this is not possible. The key will always be converted to a string. See Property Accessor docs
不,这是不可能的。密钥将始终转换为字符串。请参阅属性访问者文档
Property names must be strings. This means that non-string objects cannot be used as keys in the object. Any non-string object, including a number, is typecasted into a string via the toString method.
属性名称必须是字符串。这意味着非字符串对象不能用作对象中的键。任何非字符串对象(包括数字)都通过toString方法被类型化为字符串。
> var foo = {}
undefined
> foo[23213] = 'swag'
'swag'
> foo
{ '23213': 'swag' }
> typeof(Object.keys(foo)[0])
'string'
#2
12
In an object, no, but I have found Map extremely useful for this application. Here is where I have used it for numeric keys, a key-based event.
在一个对象中,没有,但我发现Map对此应用程序非常有用。这是我用它来做数字键的地方,这是一个基于键的事件。
onKeydown(e) {
const { toggleSidebar, next, previous } = this.props;
const keyMapping = new Map([
[ 83, toggleSidebar ], // user presses the s button
[ 37, next ], // user presses the right arrow
[ 39, previous ] // user presses the left arrow
]);
if (keyMapping.has(e.which)) {
e.preventDefault();
keyMapping.get(e.which)();
}
}
#3
7
Appears to be by design in ECMA-262-5:
看似ECMA-262-5的设计:
The Property Identifier type is used to associate a property name with a Property Descriptor. Values of the Property Identifier type are pairs of the form (name, descriptor), where name is a String and descriptor is a Property Descriptor value.
属性标识符类型用于将属性名称与属性描述符相关联。属性标识符类型的值是形式(名称,描述符)的对,其中name是String,描述符是Property Descriptor值。
However, I don't see a definite specification for it in ECMA-262-3. Regardless, I wouldn't attempt to use non-strings as property names.
但是,我在ECMA-262-3中没有看到明确的规格。无论如何,我不会尝试使用非字符串作为属性名称。
#4
0
I think you can do the following if you want to use the above thing for accessing it like as a number, I did the same and worked.
我想你可以做以下事情,如果你想使用上面的东西来访问它,就像一个数字,我做了同样的工作。
var myObj = {"0":"a","1":"b","CNT":2};
$.each(myObj,function(key,value){
if(isNaN(parseInt(key))){
return true; //continue;
}
//Code to perform operation
}
This works only when the key doesn't start with and numeric character otherwise it'll be converted to a number. See the following example:
这仅在键不以数字字符开头时才起作用,否则它将被转换为数字。请参阅以下示例:
parseInt("45kjghk") === 45
I used jQuery here
我在这里使用jQuery
Updated:
更新:
var myObj = {"0":"a","1":"b","CNT":2};
$.each(myObj,function(key,value){
if(isNaN(parseInt(key)) || (key.length !== parseInt(key).toString().length) ){
return true; //continue;
}
//Code to perform operation
}
It may overcome the above problem. Please suggest better if available and problem with this answer if there are.
它可以克服上述问题。如果有的话,请建议更好(如果有的话)和问题。
#5
-2
In JavaScript, numerical strings and numbers are interchangeable, so
在JavaScript中,数字字符串和数字是可以互换的
myObject[1] == myObject['1']
If you really want number to be the key for an object, you might want an array (i.e. created with new Array()
or []
).
如果您确实希望number为对象的键,则可能需要一个数组(即使用new Array()或[]创建)。
#1
56
No, this is not possible. The key will always be converted to a string. See Property Accessor docs
不,这是不可能的。密钥将始终转换为字符串。请参阅属性访问者文档
Property names must be strings. This means that non-string objects cannot be used as keys in the object. Any non-string object, including a number, is typecasted into a string via the toString method.
属性名称必须是字符串。这意味着非字符串对象不能用作对象中的键。任何非字符串对象(包括数字)都通过toString方法被类型化为字符串。
> var foo = {}
undefined
> foo[23213] = 'swag'
'swag'
> foo
{ '23213': 'swag' }
> typeof(Object.keys(foo)[0])
'string'
#2
12
In an object, no, but I have found Map extremely useful for this application. Here is where I have used it for numeric keys, a key-based event.
在一个对象中,没有,但我发现Map对此应用程序非常有用。这是我用它来做数字键的地方,这是一个基于键的事件。
onKeydown(e) {
const { toggleSidebar, next, previous } = this.props;
const keyMapping = new Map([
[ 83, toggleSidebar ], // user presses the s button
[ 37, next ], // user presses the right arrow
[ 39, previous ] // user presses the left arrow
]);
if (keyMapping.has(e.which)) {
e.preventDefault();
keyMapping.get(e.which)();
}
}
#3
7
Appears to be by design in ECMA-262-5:
看似ECMA-262-5的设计:
The Property Identifier type is used to associate a property name with a Property Descriptor. Values of the Property Identifier type are pairs of the form (name, descriptor), where name is a String and descriptor is a Property Descriptor value.
属性标识符类型用于将属性名称与属性描述符相关联。属性标识符类型的值是形式(名称,描述符)的对,其中name是String,描述符是Property Descriptor值。
However, I don't see a definite specification for it in ECMA-262-3. Regardless, I wouldn't attempt to use non-strings as property names.
但是,我在ECMA-262-3中没有看到明确的规格。无论如何,我不会尝试使用非字符串作为属性名称。
#4
0
I think you can do the following if you want to use the above thing for accessing it like as a number, I did the same and worked.
我想你可以做以下事情,如果你想使用上面的东西来访问它,就像一个数字,我做了同样的工作。
var myObj = {"0":"a","1":"b","CNT":2};
$.each(myObj,function(key,value){
if(isNaN(parseInt(key))){
return true; //continue;
}
//Code to perform operation
}
This works only when the key doesn't start with and numeric character otherwise it'll be converted to a number. See the following example:
这仅在键不以数字字符开头时才起作用,否则它将被转换为数字。请参阅以下示例:
parseInt("45kjghk") === 45
I used jQuery here
我在这里使用jQuery
Updated:
更新:
var myObj = {"0":"a","1":"b","CNT":2};
$.each(myObj,function(key,value){
if(isNaN(parseInt(key)) || (key.length !== parseInt(key).toString().length) ){
return true; //continue;
}
//Code to perform operation
}
It may overcome the above problem. Please suggest better if available and problem with this answer if there are.
它可以克服上述问题。如果有的话,请建议更好(如果有的话)和问题。
#5
-2
In JavaScript, numerical strings and numbers are interchangeable, so
在JavaScript中,数字字符串和数字是可以互换的
myObject[1] == myObject['1']
If you really want number to be the key for an object, you might want an array (i.e. created with new Array()
or []
).
如果您确实希望number为对象的键,则可能需要一个数组(即使用new Array()或[]创建)。