Javascript Data Type How to - Iterate through an array by the sequence stored as another array








Question

We would like to know how to iterate through an array by the sequence stored as another array.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
var alphabet = ["A","B","C","D","E","F","G","H","I","J","K"],
    sequence1 = [0,2,2,1,4];<!--from  w  w w  . ja  va 2 s  .  co m-->
for (var i = 0, l = sequence1.length; i < l; i++) {
    document.writeln('<br/>');
    document.writeln(alphabet[sequence1[i]]);
    
}

</script>
</head>
<body>

</body>
</html>

The code above is rendered as follows: