0

I'm trying to add 2 values to an array that I extract from a html page. However, the value for key [0] always returns undefined whereas I expect 4 in this case.

<script type="text/javascript">
  $(document).ready(function ()
  {
    var arrayList = $("some class");
    var x = new Array(); 
    var j = 0;
    var k = 1;

    $.each(arrayList, function(i,e) {

      var MyIf = $(e).text(); 
      x[j] = new Array();

      if(k == 2) {
        x[j][0] = 4; // This always returns undefined, no matter which value I assign.
      }

      if(k == 3) {
        x[j][1] = parseInt(MyIf);
      }

      if(k % 3 == 0) {
        j++;
        k = 1;
      } else {
        k++;
      }

    });
    console.log(x); // the console returns for all [0] "undefined"
  });
</script>

What am I missing?

5
  • 1
    How many elements does arrayList have? If it has only 1, I would expect x to be empty since you only add to it when k == 2 or k == 3. In other words, how many times does your $.each loop run? Commented Apr 23, 2014 at 9:50
  • arrayList has multiple values. Current output looks like this: [[undefined, 3], [undefined, 5], [undefined, 2]] Commented Apr 23, 2014 at 9:51
  • A fiddle will help... Commented Apr 23, 2014 at 9:52
  • Are you sure that k==2 sometimes? Commented Apr 23, 2014 at 9:53
  • what is this code trying to achieve? Commented Apr 23, 2014 at 10:05

2 Answers 2

4

It's because you create a new array every time in your each.

x[j] = new Array();

so it's always undefined.

put it in here:

if(k % 3 == 0) {
        j++;
        k = 1;
        x[j] = new Array();
      }

and don't forget to call x[j] = new Array(); before your first run.

Sign up to request clarification or add additional context in comments.

6 Comments

so how does it explain that the Array still has values for x[j][1] ?
because you make a new array an then set the value x[j][1] = somevalue;
@KrisVandenBergh Because when k == 3 it adds to the last created Array at index 1. Then, j is increased so it will not touch that same Array anymore.
@Homungus If you do x[j] = [] in the k % 3 == 0 case, x[j] will be undefined in the k == 2 and k == 3 cases above it the very first run. Perhaps not pretty, but you can do on the first line of the .each loop a if(typeof x[j] === 'undefined') x[j] = []; which fixes it (and remove it from the k % 3 == 0 case entirely).
@DaniëlKnippers you are right, of course it should be initialized before first run. updated answer.
|
0

It should be done like this:

var arrayList = [1,2,3,4,5];
var x = new Array(); 
var j = 0;
var k = 1;
x[j] = new Array();
$.each(arrayList, function(i,e) {

  var MyIf = $(e).text(); 


  if(k == 2) {
    x[j][0] = 4; // This always returns undefined, no matter which value I assign.
  }

  if(k == 3) {
    x[j][1] = parseInt(MyIf);
  }

  if(k % 3 == 0) {
    j++;
    x[j] = new Array();
    k = 1;
  } else {
    k++;
  }

});
console.log(x);

In your code, when the k variable is equal 3, you overwrite the previous value doing this:

x[j] = new Array();

  if(k == 2) {
    x[j][0] = 4; // This always returns undefined, no matter which value I assign.
  }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.