I have 3 arrays like:
我有3个数组,如:
var arr1 = [];
var arr2 = [];
var arr3 = [];
//When I want to add something into array then I use
arr1.push("text");
arr2.push("text");
but is it possible to make something like the following example?
但是可以制作类似下面的例子吗?
//example:
var arr = [];
arr['group1'].push("text1");
arr['group2'].push("text2");
arr['group2'].push("textn");
//and then iterate like:
for(item in arr['group1'])
console.log(item);
is it even possible to do something like that? I have tried but does not work.
甚至可以做那样的事情吗?我试过但不行。
4 个解决方案
#1
5
There's a fundamental misunderstanding though, arr
is an array but you're using it as an associative array, which in JavaScript is better represented with an object {}
. for...in
is for objects, NOT arrays, the MDN has a warning note about it:
但是有一个根本的误解,arr是一个数组,但你将它用作一个关联数组,在JavaScript中用对象{}更好地表示。 for ... in for objects,NOT arrays,MDN有一个关于它的警告说明:
for..in
should not be used to iterate over an Array where index order is important...for..in不应该用于迭代索引顺序很重要的数组...
I would advice even if index is trivial to use a regular for
loop or a forEach
.
即使索引使用常规for循环或forEach,我也会建议。
Consider using the following, more appropiate approach.
考虑使用以下更合适的方法。
var obj = {
group1: ['text1'],
group2: ['text2'],
group3: ['text3']
};
// pushing more strings
obj.group1.push('foo');
obj['group2'].push('baz');
#2
3
You're treating arr['group1']
as an array (by using .push()
), but you haven't declared it as an array.
您将arr ['group1']视为一个数组(使用.push()),但您尚未将其声明为数组。
var arr = [];
arr['group1'] = [];
arr['group2'] = [];
arr['group3'] = [];
arr['group1'].push("text1");
arr['group2'].push("text2");
arr['group2'].push("textn");
#3
2
It seems you're actually looking for Javascript Objects instead of arrays.
Also, you need to create these objects first.
看来你实际上是在寻找Javascript对象而不是数组。此外,您需要首先创建这些对象。
var obj = {group1:[],group2:[],group3:[]};
/* or
var obj = {};
obj.group1 = [];
*/
obj['group1'].push("text1");
// or obj.group1.push("text1");
#4
1
The for...in
structure sets your for variable to the key, not the value. Assuming arr['group1']
is an array, this will work fine:
for ... in结构将变量设置为键,而不是值。假设arr ['group1']是一个数组,这将正常工作:
//example:
var arr = [];
arr['group1'].push("text1");
arr['group2'].push("text2");
arr['group2'].push("textn");
//and then iterate like:
for(item in arr['group1'])
console.log(arr['group1'][item]);
#1
5
There's a fundamental misunderstanding though, arr
is an array but you're using it as an associative array, which in JavaScript is better represented with an object {}
. for...in
is for objects, NOT arrays, the MDN has a warning note about it:
但是有一个根本的误解,arr是一个数组,但你将它用作一个关联数组,在JavaScript中用对象{}更好地表示。 for ... in for objects,NOT arrays,MDN有一个关于它的警告说明:
for..in
should not be used to iterate over an Array where index order is important...for..in不应该用于迭代索引顺序很重要的数组...
I would advice even if index is trivial to use a regular for
loop or a forEach
.
即使索引使用常规for循环或forEach,我也会建议。
Consider using the following, more appropiate approach.
考虑使用以下更合适的方法。
var obj = {
group1: ['text1'],
group2: ['text2'],
group3: ['text3']
};
// pushing more strings
obj.group1.push('foo');
obj['group2'].push('baz');
#2
3
You're treating arr['group1']
as an array (by using .push()
), but you haven't declared it as an array.
您将arr ['group1']视为一个数组(使用.push()),但您尚未将其声明为数组。
var arr = [];
arr['group1'] = [];
arr['group2'] = [];
arr['group3'] = [];
arr['group1'].push("text1");
arr['group2'].push("text2");
arr['group2'].push("textn");
#3
2
It seems you're actually looking for Javascript Objects instead of arrays.
Also, you need to create these objects first.
看来你实际上是在寻找Javascript对象而不是数组。此外,您需要首先创建这些对象。
var obj = {group1:[],group2:[],group3:[]};
/* or
var obj = {};
obj.group1 = [];
*/
obj['group1'].push("text1");
// or obj.group1.push("text1");
#4
1
The for...in
structure sets your for variable to the key, not the value. Assuming arr['group1']
is an array, this will work fine:
for ... in结构将变量设置为键,而不是值。假设arr ['group1']是一个数组,这将正常工作:
//example:
var arr = [];
arr['group1'].push("text1");
arr['group2'].push("text2");
arr['group2'].push("textn");
//and then iterate like:
for(item in arr['group1'])
console.log(arr['group1'][item]);