Javascript Array searchIndex(n)

Description

Javascript Array searchIndex(n)


"use strict";/* w  w  w .ja  va  2s.  co  m*/
Array.prototype.searchIndex = function(n){
 /* body... */
 var result = [],
   i,
   l= this.length;
   // console.log(l);
 for( i= 0; i< l; i++){
  if(this[i] == n){
   result.push(i);
  }
 }
 if(result.length == 0){
  result.push(-1);
 }
 return result;
};


 var arr = [1,2,5,5,5,5,5,6,9];
console.log(arr.searchIndex(5));//[2,3,4,5,6]
console.log(arr.searchIndex(0));



PreviousNext

Related