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..

5
  • Yes it is possible and it is not hard to find an answer with a search Commented Apr 2, 2016 at 3:53
  • 2
    For splitting the string, How do you get a string to a character array in JavaScript? Commented Apr 2, 2016 at 3: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)? Commented Apr 2, 2016 at 3:56
  • you can simply use the length function to find the charater length of array x. Commented Apr 2, 2016 at 3: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() to condense all of that to a single expression. (The focus on splitting the string seems like an XY Problem if the length is your goal.) Commented Apr 2, 2016 at 3:58

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

what if i want to do this with all the elements and not just the one in 0 index?
@PadamChopra What's your end goal here?
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])
  }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.