Array functions in jQuery

后端 未结 8 991
小鲜肉
小鲜肉 2020-12-13 08:55

I am using jQuery in my web application. I want to use arrays, but I am not able to find out functions for arrays (add, remove or append elements in array) in jQuery. Is the

相关标签:
8条回答
  • 2020-12-13 09:09

    The Visual jQuery site has some great examples of jQuery's array functionality. (Click "Utilities" on the left-hand tab, and then "Array and Object operations".)

    0 讨论(0)
  • 2020-12-13 09:12

    Also there is a jQuery plugin that adds some methods on an array:

    Implementing Prototype’s Array Methods in jQuery

    This plugin implements Prototype's library array methods such as:

    var arr = [1,2,3,4,5,6];
    $.protify(arr, true);
    arr.all();  // true
    
    var arr = $.protify([1, 2, 3, 4, 5, 6]);
    arr.any(); // true
    

    And more.

    0 讨论(0)
  • 2020-12-13 09:14

    An easy way to get the max and min value in an array is as follows. This has been explained at get max & min values in array

    var myarray = [5,8,2,4,11,7,3];
    // Function to get the Max value in Array
    Array.max = function( array ){
    return Math.max.apply( Math, array );
    };
    
    // Function to get the Min value in Array
    Array.min = function( array ){
    return Math.min.apply( Math, array );
    };
    // Usage 
    alert(Array.max(myarray));
    alert(Array.min(myarray));
    
    0 讨论(0)
  • 2020-12-13 09:21

    You can use Underscore.js. It really makes things simple.

    For example, removing elements from an array, you need to do:

    _.without([1, 2, 3], 2);
    

    And the result will be [1, 3].

    It reduces the code that you write using jQuery.grep, etc. in jQuery.

    0 讨论(0)
  • 2020-12-13 09:25

    jQuery has very limited array functions since JavaScript has most of them itself. But here are the ones they have: Utilities - jQuery API.

    0 讨论(0)
  • 2020-12-13 09:25

    Look into the jQuery functions .grep() and .map()

    0 讨论(0)
提交回复
热议问题