I have a Div and an array with numbers in JS code, like this:
Code: Select all
<div id="dv1"></div>
<script>
var ary = [13, 2, 8, 56, 9];
// here get each ary item in reverse order: 9, 56, 8, 2, 13
</script>
Thanks.
Code: Select all
<div id="dv1"></div>
<script>
var ary = [13, 2, 8, 56, 9];
// here get each ary item in reverse order: 9, 56, 8, 2, 13
</script>
Code: Select all
<script>
var ary = [13, 2, 8, 56, 9];
var itms = '';
for(var i=ary.length; i--;) {
itms += ary[i] +', ';
}
document.getElementById('dv1').innerHTML = itms.replace(/(, )$/, ''); // replace() to removes last ','
</script>
Code: Select all
<div id="dv1"></div>
<script>
var ary = [13, 2, 8, 56, 9];
ary.reverse();
var itms = '';
for(var i=0; i<ary.length; i++) {
itms += ary[i] +', ';
}
document.getElementById('dv1').innerHTML = itms.replace(/(, )$/, ''); // replace() to removes last ','
</script>
Code: Select all
var ary = [13, 2, 8, 56, 9];
ary.reverse();
var itms = '';
ary.forEach(function(itm) {
itms += itm +', ';
});
alert(itms); // 9, 56, 8, 2, 13,