46

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?

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
asked Jan 25, 2009 at 13:45
1
  • 1
    It may be worth mentioning that jQuery objects are NOT themselves arrays. They act like arrays in many ways -- they are indexed using the eq() method and have a .length property -- but most of the standard JavaScript array methods will not work on jQuery objects. Commented Sep 17, 2011 at 20:14

8 Answers 8

83

Have a look at https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Objects/Array for documentation on JavaScript Arrays.
jQuery is a library which adds some magic to JavaScript which is a capable and featurefull scripting language. The libraries just fill in the gaps - get to know the core!

answered Jan 25, 2009 at 14:00
Sign up to request clarification or add additional context in comments.

Comments

24

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

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
answered Jan 25, 2009 at 13:50

Comments

12

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));
answered Feb 19, 2010 at 5:17

Comments

2

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".)

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
answered Jan 25, 2009 at 15:25

Comments

1

There's a plugin for jQuery called 'rich array' discussed in Rich Array jQuery plugin .

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
answered Jan 25, 2009 at 15:09

Comments

1

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.

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
answered Feb 19, 2013 at 10:01

1 Comment

Why doesn't this library extend the Array object?
0

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

Johnie Karr
2,8303 gold badges36 silver badges44 bronze badges
answered Apr 24, 2011 at 0:48

Comments

0

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.

Peter Mortensen
31.3k22 gold badges110 silver badges134 bronze badges
answered Jun 26, 2012 at 13:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.