Match and search Array value and return index with indexOf - Javascript Array Operation

Javascript examples for Array Operation:Array Search

Description

Match and search Array value and return index with indexOf

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=( function() {/* w  ww.j a  v a  2 s  .  c o m*/
var arr = ['a', 'b', 'c', 'a'];
function count(books, pattern) {
    var results = [];
    for (var index in arr) {
        if (arr[index] === pattern) {
          results.push(index);
        }
    }
    return results;
}
console.log(count(arr, 'a'));
    });

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

Related Tutorials