I have 3 arrays like:
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.
There’s a fundamental misunderstanding though,
arris an array but you’re using it as an associative array, which in JavaScript is better represented with an object{}.for...inis for objects, NOT arrays, the MDN has a warning note about it:I would advice even if index is trivial to use a regular
forloop or aforEach.Consider using the following, more appropiate approach.