Reputation: 2727
I have an array as follows:
var str_array=["hello","world"]
How to get length of string when string are there in array? I just want to get length of string "hello" and then "world" inside a loop. This is what I've tried:
for (var i=0; i<= str_array[0].length; i++){
alert(str_array[i].starAt(i));
}
I get error message
TypeError: Cannot read property 'length' of undefined
Upvotes: 0
Views: 76
Reputation: 1650
How about this:
str_array.forEach(function(e){ alert("Length = " + e.length) });
You could also use map
to get the length out of each element:
var lengths = str_array.map(function(e){ return e.length });
Upvotes: 1
Reputation: 36609
Try this:
var str_array = ["hello", "world"];
for (var i = 0, len = str_array.length; i < len; i++) {
alert('Length of ' + str_array[i] + ' is ' + str_array[i].length);
}
Upvotes: 2