We would like to know how to insert elements into array between each elements.
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!--from w w w. j a v a 2 s. c o m-->
function inject(original, val, times) {
var res = [];
for(var i=0; i < original.length; i++){
res.push(original[i]);
for(var j = 0; j < times; j++){
res.push(val);
}
}
return res;
}
var arr = [1, 2, 3, 4, 5];
var arr1 = inject(arr, 0, 3);
document.writeln(arr);
document.writeln('<br/>');
document.writeln(arr1);
</script>
</head>
<body>
</body>
</html>
The code above is rendered as follows: