Array functions in jQuery


Answers

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

Question

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 there any link related to jQuery array functions which will explain the jQuery array functions?




No, it is not possible. The Array in JS doesn't care what you've put in it, or even that the array indexes are numeric. Java, on the other hand, requires strict typing of both. I'd even go so far as to say that even Object[] is a completely different paradigm from [].




Passing an array of functions as a parameter

As with every other question of this type, i keeps on changing.

Instead, try this:

for( var i=0; i<e.length; i++) {
    (function(i) {
        // your code that depends on i not changing
    })(i);
}



Writing Multidemisional Array jQuery

Its Javascript, not JQuery that handles the arrays, so what you really want is a tutorial on multidimensional arrays in Javascript.

Here is a good one.

Basically you define one array, then reference it inside another array. For example:

var columns = new Array(3);
var rows = new Array(4);
rows[0] = columns;

This can then be accessed as follows:

rows[0][0]



The code in the callback function uses the value of i after the loop has ended, so it points to an index outside the array. You need a closure in the loop so that each iteration gets its own instance of the variable:

function test(e, f) {
  for (var i = 0; i < e.length; i++) {
    $('#clickme').append("<button id='op" + i + "'>" + e[i] + "</button>");
    (function(i){
      $('#op' + i).click(function () {
        f[i]();
      });
    })(i);
  }
}





Tags