Reputation: 39
k = [['a'], ['ab'], ['abc']];
alert(k[2].length);
The above code fragment returns 1.
How can I get the length of the string, 3 in this case?
Upvotes: 1
Views: 13949
Reputation: 6732
In your example, k
is not a normal array containing strings. It contains sub-arrays, which contain the strings. You should declare k
this way:
k = ['a', 'ab', 'abc'];
If you do want to use your own declaration, you could do this:
alert(k[2][0].length);
Upvotes: 2
Reputation:
The object is not what is expected. Consider:
k = [['a'], ['ab'], ['abc']];
a = k[2] // -> ['abc']
a.length // -> 1 (length of array)
b = k[2][0] // -> 'abc'
b.length // -> 3 (length of string)
Upvotes: 9