Array in jQuery? [closed]
How to deploy array in jQuery and java script? i need to explore in this topic? Can any one help me?
As jQuery is JavaScript, creating an array is simply:
var arr = ["an", "array"]; // array with 2 values
var arr = []; // empty array
There is never a reason to use the Array
constructor.
Read more about arrays.
Because someone mentioned associative arrays:
Although they might look like arrays, they are not arrays (in a strict JavaScript sense). This kind of data structure is normally created using objects (here with object literal notation. Same thing: There is no reason to use the Object
constructor.):
var obj = {
foo: "bar"
};
In JavaScript you can access object properties with either dot notation (obj.foo
) or array notation (obj['foo']
). This is convenient but you should not make the mistake to confuse objects and arrays.
Especially you should not mix it and attach properties to an array (like an other answer shows) if you don't know how/why it works (in short: Nearly everything in JavaScript is an object, also arrays. That does not mean that you should use them as such.).
Read more about objects.
In javascript:
var arr = new Array();
Read more about the JS Array object here.
A very simple array:
var my_array = new Array();
my_array[0] = "Hello";
my_array[1] = "World";
Which could also be set like this:
var my_array = ["Hello", "World"];
An associative array:
var my_array = new Array();
my_array["karl"] = new Array();
my_array["karl"]["age"] = 18;
my_array["viktor"] = new Array();
my_array["viktor"]["age"] = 18;
To work with arrays in jQuery you have for example the method each()
that can be used like this:
$(my_array).each(function(item){
// here you can interact with the item
});
精彩评论