Accessing character of an Element of An Array

Accessing character of an Element of An Array
0

#1

Ok here is an example of what I mean:
So lets say there is an array of numbers: var arr = [123, 345, 456, 678, 789];,
And lets say I want to access 1, from the first element of the Array.

How do I do that? arr[0].charAt(0) doesn’t work. Neither does arr[0][0] I have been searching google for the past 20 minutes and cant find anything, If anyone has any idea on how to do this, It would be greatly appreciated!


#2

Can’t test this, but maybe the following will work:

arr[0].toString().charAt(0)


#3

Wow, that actually worked! Thanks a lot!


#4

You have to convert a number to string in order to access specific characters.There are multiple ways to do so :

var arr = [123, 345, 456, 678, 789];

String(arr[0]).charAt(0);
('' + arr[0]).charAt(0);
arr[0].toString().charAt(0);