Javascript Data Type How to - Do IndexOf a whole Array and output names








Question

We would like to know how to do IndexOf a whole Array and output names.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!--from ww  w. ja  v a2  s. c o m-->
var NHL = new Array("N", "New", "New York", "Ph", "Penguins", "Boston", "Buffalo", "Montreal" , 
                    "Ottawa", "Toronto"); 

for( var i=0 ; i < NHL.length; i++ ) {
     if ( NHL[i].indexOf('ew') !== -1 ) {
            document.writeln( "This " + NHL[i] + " Contains 'an' at index: " + i );
            document.writeln('<br/>');
     }
}

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

The code above is rendered as follows: