0

I have an array that has a bunch of content separated by colons, so for example initArray[0] might have the content 10:30:20:10. How do I split this again so I can have initArray[0][1]. Any help is appreciated!

Felix Kling
820k181 gold badges1.1k silver badges1.2k bronze badges
asked Mar 14, 2012 at 14:15
1
  • I tried a bunch of things but I couldn't get it to work at all. Javascript is not my forte. Commented Mar 14, 2012 at 14:31

3 Answers 3

3

If you want to split every element in the array:

for( var i = 0; i < initArray.length; i++ ) {
 initArray[i] = initArray[i].split( ':' );
}

So this:

[ '10:30:20:10', 'a:b:c:d' ]

becomes:

[ [ '10', '30', '20', '10' ], [ 'a', 'b', 'c', 'd' ] ]
answered Mar 14, 2012 at 14:18
Sign up to request clarification or add additional context in comments.

1 Comment

oh god, I realised what I was doing wrong, I had the i and initArray.length the wrong way around in my for loop. I thought my syntax was flawed.
1
initArray=new Array('10:30:20:10', '11:31:21:11');
for(i=0;i<initArray.length;i++)
{
 initArray[i]=initArray[i].split(':');
}
console.log(initArray[0][0]); // 10
console.log(initArray[1][0]); // 11​​​​​​​​
answered Mar 14, 2012 at 14:25

Comments

0
var tmp = "10:30:20:10".split(":")
alert(tmp[0]) ---> "10"
answered Mar 14, 2012 at 14:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.