0

Suppose I have the following array- var x= ["hello"];

Can i further break it into a character array like this one- var x_character= ["h","e","l","l","o"];

if not, can you tell me how to know the character length of the x array..

  • Yes it is possible and it is not hard to find an answer with a search – R. Schifini Apr 02 '16 at 03:53
  • 2
    For splitting the string, [How do you get a string to a character array in JavaScript?](http://stackoverflow.com/questions/4547609/how-do-you-get-a-string-to-a-character-array-in-javascript) – Jonathan Lonowski Apr 02 '16 at 03:54
  • 1
    Is there any reason you can't use the .length method to get the information you are after (using unusual characters or such)? – Chris Apr 02 '16 at 03:56
  • you can simply use the length function to find the charater length of array x. – Akhilesh Singh Apr 02 '16 at 03:56
  • To find the total length of all strings in the array (assuming there are potentially more than 1). Start a variable at `0`, loop through the array, and add to the variable the `length` of each string. Try [`.reduce()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) to condense all of that to a single expression. (The focus on splitting the string seems like an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) if the length is your goal.) – Jonathan Lonowski Apr 02 '16 at 03:58

2 Answers2

0

Yes, use the .split() method:

var x = ["hello"]
x[0].split("")  

Returns the array ["h","e","l","l","o"]. The "" argument means to split the string at each empty substring.

digglemister
  • 1,477
  • 3
  • 16
  • 31
0

Create a new empty array, iterate through the original array and push each character into the new array. To get the length of the original array - use "x.length" (ie: "var arrayLength=x.length";)

var x = ["hello"];
var x_character=[];

for(i=0;i<x.length;i++)
  {
    var x_character.push(x[i])
  }
gavgrif
  • 15,194
  • 2
  • 25
  • 27