When I create a new javascript array, and use an integer as a key, each element of that array up to the integer is created as undefined. for example:
当我创建一个新的javascript数组并使用一个整数作为键时,到这个整数为止的数组的每个元素都被创建为未定义的。例如:
var test = new Array();
test[2300] = 'Some string';
console.log(test);
will output 2298 undefined's and one 'Some string'.
将输出2298个未定义的和一个'Some string'。
How should I get javascript to use 2300 as a string instead of an integer, or how should I keep it from instanciating 2299 empty indices?
如何让javascript使用2300作为字符串而不是整数,或者如何避免它实例化2299空索引?
9 个解决方案
#1
109
Use an object, as people are saying. However, note that you can not have integer keys. JavaScript will convert the integer to a string. The following outputs 20, not undefined:
用一个物体,就像人们说的那样。但是,请注意,不能使用整型键。JavaScript将把整数转换为字符串。以下输出20,未定义:
var test = {}
test[2300] = 20;
console.log(test["2300"]);
#2
31
You can just use an object:
你可以使用一个对象:
var test = {}
test[2300] = 'Some string';
#3
19
As people say javascript will convert an string of number to integer so is not possible to use directly on an associative array, but objects will work for you in similar way I think.
正如人们所说,javascript将把一串数字转换成整数,因此不可能直接在关联数组中使用,但是我认为对象将以类似的方式为您工作。
You can create your object:
你可以创建你的对象:
var object = {};
and add the values as array works:
并将值添加为数组工作:
object[1] = value;
object[2] = value;
this will give you:
这将给你:
{
'1':value,
'2':value
}
After that you can access it like array in other languages getting the key:
之后你可以像其他语言的数组一样访问它,获取密钥:
for(key in object)
{
value = object[key] ;
}
I hope this is useful! I have tested and works.
我希望这是有用的!我已经测试和工作了。
#4
7
If the use-case is storing data in a collection then ES6 provides the Map
type.
如果用例是在一个集合中存储数据,那么ES6提供映射类型。
It's only heavier to initialize.
初始化会更重。
Here is an example:
这是一个例子:
const map = new Map();
map.set(1, "One");
map.set(2, "Two");
map.set(3, "Three");
console.log("=== With Map ===");
for (const [key, value] of map) {
console.log(`${key}: ${value} (${typeof(key)})`);
}
console.log("=== With Object ===");
const fakeMap = {
1: "One",
2: "Two",
3: "Three"
};
for (const key in fakeMap) {
console.log(`${key}: ${fakeMap[key]} (${typeof(key)})`);
}
Result:
结果:
=== With Map ===
1: One (number)
2: Two (number)
3: Three (number)
=== With Object ===
1: One (string)
2: Two (string)
3: Three (string)
#5
3
Get the value for an associative array property when the property name is an integer:
当属性名为整数时,获取关联数组属性的值:
Starting with an Associative Array where the property names are integers:
从属性名称为整数的关联数组开始:
var categories = [
{"1":"Category 1"},
{"2":"Category 2"},
{"3":"Category 3"},
{"4":"Category 4"}
];
Push items to the array:
将项推到数组:
categories.push({"2300": "Category 2300"});
categories.push({"2301": "Category 2301"});
Loop through array and do something with the property value.
遍历数组并对属性值做一些操作。
for (var i = 0; i < categories.length; i++) {
for (var categoryid in categories[i]) {
var category = categories[i][categoryid];
// log progress to the console
console.log(categoryid + " : " + category);
// ... do something
}
}
Console output should look like this:
控制台输出应该是这样的:
1 : Category 1
2 : Category 2
3 : Category 3
4 : Category 4
2300 : Category 2300
2301 : Category 2301
As you can see, you can get around the associative array limitation and have a property name be an integer.
如您所见,您可以绕过关联数组限制,并拥有一个属性名为整数。
NOTE: The associative array in my example is the json you would have if you serialized a Dictionary<string, string>[] object.
注意:在我的示例中,关联数组是序列化字典
#6
2
Try using an Object, not an Array:
尝试使用对象,而不是数组:
var test = new Object(); test[2300] = 'Some string';
#7
2
Use an object instead of an array. Arrays in JavaScript are not associative arrays. They are objects with magic associated with any properties whose names look like integers. That magic is not what you want if you're not using them as a traditional array-like structure.
使用对象而不是数组。JavaScript中的数组不是关联数组。它们是具有魔力的对象,与任何名称看起来像整数的属性相关联。如果你不把它们作为传统的类数组结构来使用,那么这种魔法就不是你想要的。
var test = {};
test[2300] = 'some string';
console.log(test);
#8
1
Sometimes i use a prefixes for my keys. For example:
有时我在钥匙上用前缀。例如:
var pre = 'foo',
key = pre + 1234
obj = {};
obj[ key ] = val;
Now you have no Problem accessing them.
现在访问它们没有问题。
#9
0
Use an object - with an integer as the key - rather than an array.
使用对象——以整数作为键——而不是数组。
#1
109
Use an object, as people are saying. However, note that you can not have integer keys. JavaScript will convert the integer to a string. The following outputs 20, not undefined:
用一个物体,就像人们说的那样。但是,请注意,不能使用整型键。JavaScript将把整数转换为字符串。以下输出20,未定义:
var test = {}
test[2300] = 20;
console.log(test["2300"]);
#2
31
You can just use an object:
你可以使用一个对象:
var test = {}
test[2300] = 'Some string';
#3
19
As people say javascript will convert an string of number to integer so is not possible to use directly on an associative array, but objects will work for you in similar way I think.
正如人们所说,javascript将把一串数字转换成整数,因此不可能直接在关联数组中使用,但是我认为对象将以类似的方式为您工作。
You can create your object:
你可以创建你的对象:
var object = {};
and add the values as array works:
并将值添加为数组工作:
object[1] = value;
object[2] = value;
this will give you:
这将给你:
{
'1':value,
'2':value
}
After that you can access it like array in other languages getting the key:
之后你可以像其他语言的数组一样访问它,获取密钥:
for(key in object)
{
value = object[key] ;
}
I hope this is useful! I have tested and works.
我希望这是有用的!我已经测试和工作了。
#4
7
If the use-case is storing data in a collection then ES6 provides the Map
type.
如果用例是在一个集合中存储数据,那么ES6提供映射类型。
It's only heavier to initialize.
初始化会更重。
Here is an example:
这是一个例子:
const map = new Map();
map.set(1, "One");
map.set(2, "Two");
map.set(3, "Three");
console.log("=== With Map ===");
for (const [key, value] of map) {
console.log(`${key}: ${value} (${typeof(key)})`);
}
console.log("=== With Object ===");
const fakeMap = {
1: "One",
2: "Two",
3: "Three"
};
for (const key in fakeMap) {
console.log(`${key}: ${fakeMap[key]} (${typeof(key)})`);
}
Result:
结果:
=== With Map ===
1: One (number)
2: Two (number)
3: Three (number)
=== With Object ===
1: One (string)
2: Two (string)
3: Three (string)
#5
3
Get the value for an associative array property when the property name is an integer:
当属性名为整数时,获取关联数组属性的值:
Starting with an Associative Array where the property names are integers:
从属性名称为整数的关联数组开始:
var categories = [
{"1":"Category 1"},
{"2":"Category 2"},
{"3":"Category 3"},
{"4":"Category 4"}
];
Push items to the array:
将项推到数组:
categories.push({"2300": "Category 2300"});
categories.push({"2301": "Category 2301"});
Loop through array and do something with the property value.
遍历数组并对属性值做一些操作。
for (var i = 0; i < categories.length; i++) {
for (var categoryid in categories[i]) {
var category = categories[i][categoryid];
// log progress to the console
console.log(categoryid + " : " + category);
// ... do something
}
}
Console output should look like this:
控制台输出应该是这样的:
1 : Category 1
2 : Category 2
3 : Category 3
4 : Category 4
2300 : Category 2300
2301 : Category 2301
As you can see, you can get around the associative array limitation and have a property name be an integer.
如您所见,您可以绕过关联数组限制,并拥有一个属性名为整数。
NOTE: The associative array in my example is the json you would have if you serialized a Dictionary<string, string>[] object.
注意:在我的示例中,关联数组是序列化字典
#6
2
Try using an Object, not an Array:
尝试使用对象,而不是数组:
var test = new Object(); test[2300] = 'Some string';
#7
2
Use an object instead of an array. Arrays in JavaScript are not associative arrays. They are objects with magic associated with any properties whose names look like integers. That magic is not what you want if you're not using them as a traditional array-like structure.
使用对象而不是数组。JavaScript中的数组不是关联数组。它们是具有魔力的对象,与任何名称看起来像整数的属性相关联。如果你不把它们作为传统的类数组结构来使用,那么这种魔法就不是你想要的。
var test = {};
test[2300] = 'some string';
console.log(test);
#8
1
Sometimes i use a prefixes for my keys. For example:
有时我在钥匙上用前缀。例如:
var pre = 'foo',
key = pre + 1234
obj = {};
obj[ key ] = val;
Now you have no Problem accessing them.
现在访问它们没有问题。
#9
0
Use an object - with an integer as the key - rather than an array.
使用对象——以整数作为键——而不是数组。