user7339347
user7339347

Reputation:

JAVASCRIPT : for loop array undefined -1 index

var alldivstamp = document.getElementsByClassName("divs");
for(var i = 0; i < array.length; i++){
    if(array[i-1].getAttribute("data") > 1){
        //error here = TypeError: array[(i - 1)] is undefined
    }
}

error output: "TypeError: array[(i - 1)] is undefined"

for(var i = 0; i < array.length; i++){
    if (typeof foo !== 'undefined') {
        if(array[i-1].getAttribute("data") > 1){
            //error here = TypeError: array[(i - 1)] is undefined
        }
    }   
}

this did not fix it

for(var i = 0; i < array.length; i++){
    if (typeof foo !== 'undefined' && i < 1 && i > array.length-1) {
        if(array[i-1].getAttribute("data") > 1){
            //error here = TypeError: array[(i - 1)] is undefined
        }
    }   
}

error here too

undefined value in if statement breaks for loop

here is the data structure

<div class="divs"></div>
<div class="divs"></div>
<div class="divs"></div>
.....

when i=0 it checks for -1 in index
"therefor array index does not exist cousing for loop to stop"

SOLUTION:

for(var i = 0; i < array.length; i++){
    if (i > 0) {
        if(array(i-1).getAttribute("data") > 1){
            //do ...
        }
    }if(i == 0){
        //do ...
    }   
}

success!


error : (TypeError: "x" is (not) "y")

TypeError: "x" is (not) "y"

Examples:
TypeError: "x" is undefined
TypeError: "x" is null
TypeError: "undefined" is not an object
TypeError: "x" is not an object or null
TypeError: "x" is not a symbol

Upvotes: 3

Views: 806

Answers (6)

user7339347
user7339347

Reputation:

for(var i = 0; i < array.length; i++){
    if (i > 0) {
        if(array(i-1).getAttribute("data") > 1){
            //do ...
        }
    }if(i == 0){
        //do ...
    }   
}

Upvotes: 0

Saman Hajizade
Saman Hajizade

Reputation: 270

your loop does not run. like:

 var i = 0; i < array.length; i++

but run like this:

 var i = 0; i <= array.length; i++

perevent from error do this:

for(var i = 0; i <= array.length; i++){
    var data = array[i];
    if(data){
      if(data.getAttribute("data") > 1){
        // your code
      }
    }
}

or

for(var i = 1 ; i <= array.length + 1; i++){
    var data = array[i - 1];
    if(data){
      if(data.getAttribute("data") > 1){
        // your code
      }
    }
}

Upvotes: 1

andre mcgruder
andre mcgruder

Reputation: 1520

You DOM selector returns an array based on your "divs" class. That is the array you need to do your dot length for.

UPDATE

You are checking to see if the element has the attribute. You should use the "hasAttribute" method.

Your code....

var alldivstamp = document.getElementsByClassName("divs");
for(var i = 0; i < array.length; i++){
    if(array[i-1].getAttribute("data") > 1){
        //error here = TypeError: array[(i - 1)] is undefined
    }
}

Should be...

var alldivstamp = document.getElementsByClassName("divs");
for(var i = 0; i < alldivstamp.length; i++){
    if(alldivstamp[i].hasAttribute("data") === false){
        //error here = TypeError: array[(i - 1)] is undefined
    }
}

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386883

According to document.getElementsByClassName, you are getting an array like object with elments.

You could use the index for an access of the item with

array[i]

and for a property, you could use dot notation, like

array[i].foo

or bracket notation

array[i]['foo']

or a method like getAttribute.

array[i].getAttribute('data')

A valid loop, could be this

array = document.getElementsByClassName("divs");
for (var i = 0; i < array.length; i++){
    if (array[i].getAttribute('data')) { // check for truthyness
        // do something
    }
}

Upvotes: 1

Naghaveer R
Naghaveer R

Reputation: 2944

For the first iteration i=0, then array[i-1] = array[-1]. this is doesn't exists.

This can be something like this

for(var i = 1; i < array.length; i++){
    if(array[i-1].getAttribute("data") > 1){
        //success
    }
}

Or

for(var i = 0; i < array.length; i++){
    if(array[i].getAttribute("data") > 1){
        //success
    }
}

Upvotes: 0

mrid
mrid

Reputation: 5796

Indexes in array start from 0. When i=0, array(i-1) equals array(-1) which is undefined. And by the way to get an array value, you use square brackets, not circular brackets . eg. arr[1]

So you should use :

for(var i = 0; i < array.length; i++){
    if(array[i].getAttribute("data") > 1){
        // your code here
    }
}

Upvotes: 0

Related Questions