This is pretty cut and dry, just set up a nested loop:
var count = 1;
var twoDimensionalArray =[];
for (var i=0;i<2;i++)
{
var data = [];
for (var j=0;j<5;j++)
{
data.push("Test" + count);
count++;
}
twoDimensionalArray.push(data);
}
Answer from Brian on Stack OverflowThis is pretty cut and dry, just set up a nested loop:
var count = 1;
var twoDimensionalArray =[];
for (var i=0;i<2;i++)
{
var data = [];
for (var j=0;j<5;j++)
{
data.push("Test" + count);
count++;
}
twoDimensionalArray.push(data);
}
It sounds like you want to map the array of text for each $something element into an outer jagged array. If so then try the following
var outterArray = [];
$something.each(function () {
var innerArray = [];
$(this).somethingElse.each(function () {
innerArray.push($(this).text());
});
outterArray.push(innerArray);
});
alert(outterArray);
How to create a dynamic 2D array in javascript - Stack Overflow
javascript - How to create dynamic two dimensional array in jquery or js - Stack Overflow
javascript multi dimensional dynamic array - Stack Overflow
javascript - constructing two dimensional array dynamically - Stack Overflow
Practically? Yes. You can create an array of arrays which functions as an 2D array as every item is an array itself:
let items = [
[1, 2],
[3, 4],
[5, 6]
];
console.log(items[0][0]); // 1
console.log(items[0][1]); // 2
console.log(items[1][0]); // 3
console.log(items[1][1]); // 4
console.log(items);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
But technically this is just an array of arrays and not a โtrueโ 2D array, as I. J. Kennedy pointed out.
It should be noted that you could keep nesting arrays into one another and so create โmultidimensionalโ arrays.
You simply make each item within the array an array.
var x = new Array(10);
for (var i = 0; i < x.length; i++) {
x[i] = new Array(3);
}
console.log(x);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
If you mean an object (as in your example), and want to use variables as keys, you'll have to split the declaration in multiple lines, and use bracket notation:
var tblObj = { main1: {}, main2: {} };
tblObj.main1[var2] = var3;
tblObj.main1[var3] = var4;
tblObj.main2[var5] = var6;
(Assuming all those variables are already defined.)
Sure, you can define a multi-dimensional array in one line, exactly as you are, just using the [] array notation. {} is for objects.
var multidim = [
[1,2,3],
[4,5,6],
[7,8,9]
];
if (!globalArray[index])
globalArray[index] = []; // init the array.
globalArray[index].push(name);
You have a typo with the dot:
$.("#uname").val();
Change to:
$("#uname").val();
What are you trying to do with this code?
Update: (The question was totally edited.)
Your code:
var globalArray[0] = new Array();
globalArray[0] is invalid variable name, you need first to declare the array:
var globalArray = []; // Array literal.
globalArray[0] = [] // The element at position 0 is new an array.
Intead of
if(loop == 0){
globalArray[index][0] = uname;
}else{
globalArray[index][loop++] = uname;
}
Use this
if(loop > 0){
globalArray[index][loop++] = uname;
}else{
globalArray[index][0] = uname;
}
You do not Dim the Array to an actual size,
You can just set an variable on any index to whatever you want
if you want two Dimensions you just could define an array
var arr = []
set an element to another array
arr[0] = []
and put an value inside an element in the "2nd dimension"
arr[0][10] = 10
You only have the elements in the "first dimension" to be an array
You could also do things like placing arrays into arrays
like
var arr = []
var arr1 = [1,2,3]
arr[0] = arr1
console.log(arr[0][2]) //3
And if you have an array and want to push elements in an inner array just do a check if the element is defined, like
var arr = []
for ( var i = 0; i < 5 ; i++) {
if(!arr[i])
arr[i] = []
for ( var j = 0; j < 3 ; j++)
arr[i][j] = i + " - " + j // Or like this,
//arr[i].push(i + " " + j) //
}
console.log( arr[2][3]) // 2 - 3
Given the Code you posted
function newTest() {
var myArray = [[],[]];
// myArray[] = new Array(14);
var recCount = 15
var n =0;
var i = 0;
var s = 0;
for (i = 0; i < recCount; i++) {
if(!myArray[i])
myArray[i] = []
for (s = 0; s < 15; s++) {
console.log(i)
// myArray[i] = new Array(14);
myArray[i][s] = s;
n++;
// alert("i=" + i + " s=" + s + " Val: " + (myArray[i][s]));
}
// var u = 4;
s = 0;
alert(myArray[0][3]);
alert(myArray[0][4]);
alert("i=" + i + " s=" + s + " Val: " + (myArray[i][s]));
}
}
newTest()
This works for me
โ
All arrays are dynamic in javascript. You can do things like
var list = [];
list[15] = 15;
And it will simply expand the array to have a length of 16, with the values for the first 15 indexes being undefined (~ null). Thus, your 3 examples of multi-dimensional arrays should all work.
Edit: Just saw that you included your code. You are not initializing the arrays of your first dimension. Your loop should be
for (i = 0; i < recCount; i++) {
//If the outer array does not have an inner array at this position already, create one
if (!myArray[i])
myArray[i] = []; //!!!
for (s = 0; s < 15; s++) {
// myArray[i] = new Array(14);
myArray[i][s] = coordsAry[n]; //myArray[i] returned null/undefined, when i got bigger then the declared array.
n++;
// alert("i=" + i + " s=" + s + " Val: " + (myArray[i][s]));
}
// var u = 4;
s = 0;
alert(myArray[0][3]);
alert(myArray[0][4]);
alert("i=" + i + " s=" + s + " Val: " + (myArray[i][s]));
}
Pure JavaScript:
var newData = [];
var sampledata = [10,20,30,40];
for (var i = 0; i < sampledata.length; i++) {
newData.push([i, sampledata[i]]);
}
Using higher-order functions:
var newData = sampledata.map(function(el, i) {
return [i, el];
})
if sampledata is an array
var sampledata = [10,20,30,40]
var newData = []
jQuery.each(sampledata,function(i,data){newData.push([i,data])})