jquery grep(),inArray(), map() and join() in javascript

1.jquery grep(): Filtered through an array

   eg.
       var array = [1,2,3,4,5,6,7,8,9];
       var filterarray = $.grep(array,function(value){
           return value > 5;//filter out of more than 5
       });
       for(var i=0;i<filterarray.length;i++){
           alert(filterarray[i]);
       }
       for (key in filterarray){
           alert(filterarray[key]);
       }

2.jquery inArray(): return index in anArray.

   eg.
       var anArray = ['one','two','three'];
       var index = $.inArray(‘two’,anArray);
       alert(index);
       alert(anArray[index]);//value is two

3.jquery map(): return a new array.

   eg. 
       var strings = ['0','1','2','3','4','S','6'];
       var values = $.map(strings,function(value){
               var result = new Number(value);
               return isNaN(result) ? null:result;//isNaN:is Not a Number的缩写
           }
       );
       for (key in values) {
           alert(values[key]);
       }

4.jquery join(): join string by a char.

   eg.
      var arr = [ "a", "b", "c", "d", "e" ];
      document.write(arr.join("-")); 
      result:a-b-c-d-e.

5.jquery each():Traversing an array(array or obj)

   eg. 
        var anObject = {one:1,two:2,three:3};
       $.each(anObject,function(name,value) {
           alert(name);
           alert(value);
       });
       var anArray = ['one','two','three'];
       $.each(anArray,function(n,value){
           alert(n);
           alert(value);
       }
       );

Pingbacks are closed.

Comments are closed.