0

I am working on scraping part and i am getting the data well, but when I am iterating the array and trying to get the data throwing me TypeError: Cannot read property '3' of undefined

My code is:

  $('table#blob1 tr.insRow').filter(function(){
            var data = $(this);
            i = i + 1;
            bow_arr[i] = new Array(6);
            bow_arr[i][1] = data.children(1).text();
            bow_arr[i][2] = data.children(2).text();
            bow_arr[i][3] = data.children(3).text();
            bow_arr[i][4] = data.children(4).text();
            bow_arr[i][5] = data.children(5).text();
            bow_arr[i][6] = data.children(6).text();    
   })

Here i am creating two dimensional array and inserting the data into it. and i am able to get all the children values correctly and i have inserted all those data into the two dimensional array.

The filter function runs for 5 times since it is encountered five times.

 for(i=0;i<1;i++){
     console.log(bow_arr[i][3]+" - "+bow_arr[i][4]);
  }

The above code i am just trying to print the values but i am getting TypeError like this. TypeError: Cannot read property '3' of undefined

2
  • 4
    FYI Indices in JavaScript start with 0. Commented May 7, 2014 at 11:24
  • 2
    Unrelated tip: nobody uses the new Array(6) notation nowadays. It's lengthy and it's easy to use it wrong inadvertently. Just [] will do. Commented May 7, 2014 at 11:26

2 Answers 2

2

According your code, your iterator loop is written in a wrong way.

In your code you have

i = i + 1;

Which makes your array look like this :

bow_arr[0] === undefined; // equals true

bow_arr[1] === [ 'text from child 1', 'text from child 2' ... ]
bow_arr[2] === [ 'text from child 1', 'text from child 2' ... ]

And your iterator

for(i=0;i<1;i++) { ... }

Will iterate only 0 one time.

You have two possibilities. To fix your code or to fix your iterator

I choose to fix your iterator, which should look like :

for(i=1;i<bow_arr.length;i++){
   console.log(bow_arr[i][3]+" - "+bow_arr[i][4]);
}

Just to make sure you can also include a check to see if this item in the array is undefined.

for(i=1;i<bow_arr.length;i++) {
   if ( !bow_arr[i] || !bow_arr[i][3] || !bow_arr[i][4] ) continue;
   console.log(bow_arr[i][3]+" - "+bow_arr[i][4]);
}
Sign up to request clarification or add additional context in comments.

Comments

1

As VisioN says you are indexing 0 but you have not entered anything into that post. Initiate i as 0 and you should be fine :). Move incrementation to end of filter function.

  var i = 0;

  $('table#blob1 tr.insRow').filter(function(){
            var data = $(this);
            bow_arr[i] = new Array(6);
            bow_arr[i][1] = data.children(1).text();
            bow_arr[i][2] = data.children(2).text();
            bow_arr[i][3] = data.children(3).text();
            bow_arr[i][4] = data.children(4).text();
            bow_arr[i][5] = data.children(5).text();
            bow_arr[i][6] = data.children(6).text();    
            i = i + 1;
   })

Fyi: You loop ONCE in the code below = What's the point of having a loop? :)

 for(i=0;i<1;i++){
     console.log(bow_arr[i][3]+" - "+bow_arr[i][4]);
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.