Javascript Data Type How to - Split string twice into array








Question

We would like to know how to split string twice into array.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!--from ww  w  . j  a  v  a 2 s.co  m-->
var yourString = '1,2,3;4,5,6;7,8,9;a,b,c;d,e,f;g,h,i';
var array = [];
yourString.split(';').forEach(function(value) {
  array.push(value.split(','));
});
document.writeln(array);

</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: