-1

I want to create array for each user and store them in another array. For that I used this code:

var arrs = [];
var userCount = 1;
@foreach($eventUsers as $user)
{
   arrs['arr' + userCount] = [];
   userCount++;
}

and console.log(arrs); gave me the output

[arr1: Array[0], arr2: Array[0], arr3: Array[0], arr4: Array[0]]

Then I need to push elements to each array(arr1, arr2, arr3, arr4) while looping through 'arrs'

I tried using:

for (arr in arrs) {
    arr.push('x');
}

But didn't work. Can anyone give me a solution?

Opal
  • 81,889
  • 28
  • 189
  • 210
SNT93
  • 431
  • 1
  • 6
  • 19
  • possible duplicate of [Is there any key/value pair structure in JavaScript?](http://stackoverflow.com/questions/6771763/is-there-any-key-value-pair-structure-in-javascript) – Blue Jun 08 '15 at 08:50
  • 1
    What language is this? In any case, instead of using keys `arr1` etc., just use array indexes. –  Jun 08 '15 at 09:43
  • @SNT93, did you disappear? This is the second question you've posted and then disappeared. You should accept questions if they answer your problem, so you can gain rep on the site, and support those who help you (And also let people know that we've solved your problem). – Blue Jun 08 '15 at 09:50

3 Answers3

1

Found an answer..

 for (key in arrs) {
  arrs[key].push("RRRRR");
}
SNT93
  • 431
  • 1
  • 6
  • 19
1

Build your array

var arrs = {}, i;
for (i = 1; i <= 4; i++) {
    arrs['arr' + i] = [];
}

update the arrs

// iterate over the keys
for (i in arrs) {
    // make your assignment
    arrs[i].push('x');
};
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

What you want is an object:

var obj = {};
obj.test = "a string";
obj["test2"] = "another string";
console.log(obj);

Check this post for how to loop though an object, and this post for more info.

Community
  • 1
  • 1
Blue
  • 22,608
  • 7
  • 62
  • 92