jQuery creating a multidimensional array on the fly

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


jQuery creating a multidimensional array on the fly



I'm trying to use jQuery to create the below sample array I want to output:


[["foo0","foo1"],["foo2","foo3","foo4"],["foo5"]]



Code I'm trying to use:


var counter = 0;
var arr = ;
$('.unknown-number-of-elements').each(function(){
var keyNumber = $(this).val();
var valToPush = "foo"+counter;
if(keyNumber in arr){
arr[keyNumber].push(["'"+ valToPush +"'"]);
}else{
arr[keyNumber] = valToPush;
}
counter++;
});
console.log(arr);



The code above is giving the following error:



Uncaught TypeError: arr[keyNumber].push is not a function



Basically if the array key already exists I would like to create a sub array and add values to that sub array.





Check this, there is no in operator in javascript
– Durga
57 secs ago











By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

'Series' object is not callable Error / Statsmodels illegal variable name