1

I hope you'll help me with this. I have the following piece of code:

var m = new Array([],[]);
var p=1; 
while(p<=20){
  x=formula.;
  y=formula.;
  m.push(x);
  m[p-1].push(y) //here spits and error - Uncaught Type Error: m[(p - 1)].push is not a function
  p++;
 ...
}

I have also tried 'm[p][0]=x'; 'm[p][1]=y;' but on m[p][0]... throws an error because the sector 0 was undefined or sth like this.

Rino Raj
  • 6,264
  • 2
  • 27
  • 42
KDX2
  • 945
  • 2
  • 14
  • 31
  • m is just an array with 2 empty arrays inside, so anything above m[1] will give error – juvian Aug 19 '15 at 20:50
  • I might help to tell us a bit about what you're trying to accomplish. What's the goal? – showdev Aug 19 '15 at 20:51
  • The two dimensional array you have created `m` only has two 'rows'. You first must fill the array up with a number of empty arrays - more than just the two you have done in your code (the `new Array([],[])`). You can achieve this with a simply for loop. Also, try to avoid the `new Array(...)` syntax where possible (it is rarely needed) and instead opt for the `[...]` syntax. – Mahout Aug 19 '15 at 20:53
  • i'm trying to create a two dimensional array and on the first row to push all the x-es on each loop and on row 1 to push all the y-s on a loop, until looping is finished. – KDX2 Aug 19 '15 at 20:55
  • As you are trying to push data into 20 arrays, you need 20 arrays: `var m = new Array([],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]);`. – Guffa Aug 19 '15 at 20:56
  • similar question: http://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript – Octopus Aug 19 '15 at 21:00

2 Answers2

1

I believe you are trying to do something like this:

var m = new Array();
var p=1; 
while(p<=20){
  m.push([]) // we add an empty array to m so p-1 position exists
  m[p-1].push(p) // we add an element to m[p-1]
  p++;
}

console.log(m)
juvian
  • 15,875
  • 2
  • 37
  • 38
  • sth like this but where do we get and save x and y? – KDX2 Aug 19 '15 at 21:07
  • @KDX2 either change .push(p) for .push(x,y) or you can use an object and do .push({ x : x , y : y }) and then access like m[0].x, m[2].y – juvian Aug 19 '15 at 21:10
1
var m = [];
var p=1; 
while(p<=20){
  x=formula;
  y=formula;
  m.push([x,y]);
  p++;
}

Notes:

  1. Only rarely do you need Array(), use [] instead, shorter.
  2. All arrays are single dimensional, but you can nest arrays
  3. You can build an array from variables in place like [x,y]
  4. The first calculation's x will be in m[0][0] and the first calculation's y will be in m[0][1]
  5. This yields m=[[x1,y1],[x2,y2],[x3,y3],...]`

If instead you want m=[[x1,x2,x3,...],[y1,y2,y3,...]] that should be coded differently:

var xs=[],ys=[];
var p=1; 
while(p<=20){
  x=formula;
  y=formula;
  xs.push(x);
  ys.push(y);
  p++;
}

var m = [xs,ys];
Paul
  • 26,170
  • 12
  • 85
  • 119
  • I decided to use the first method you proposed. I hope it pushes well but when it comes up to reading the array it returns me one and the same values... See how I'm reading it: 'while(i<=m.length){ xo=m[i][0]; yo=m[i][1];' – KDX2 Aug 21 '15 at 09:20
  • @KDX2 That's too little to go on. It could be several different things. Perhaps try asking a new question. That will attract new people to the issue. – Paul Aug 21 '15 at 14:27