jQuery Arrays | Create, Add To, Remove From and Compare

Creating Arrays in jQuery

At the very basic level you can create an empty array like this

var myArray = [] ;

I like to use empty arrays because I will fill them programmatically with HTML that is available on the page. Let’s say you have created a list from a database that looks like this

<ul id="userList">
<li id="user1">Jim</li>
<li id="user2">Mike</li>
<li id="user3">Jake</li>
</ul>

Filling an Empty Array with data from an Unordered List

Using the list above we can put the users ID and Names into the array for use later

// we are using the array created above
var userList = $('#userList');
userList.children('li').each(function() {
var userID = $(this).attr('id');
var userName = $(this).text();
myArray.push({
'id':userID,
'name':userName
});
});

Display Data in a jQuery Array

Once the data is in the array you will want to make sure it looks good. Here’s how you can show the data in the array quickly.

var result = $('body');
$.each(myArray, function(i, v) {
result.append($('<p>').hide().text('Name: ' + v.name + ' with ID: ' + v.id).show('slow'));
});

I am basically creating a P tag for each item in the array and creating text in each paragraph with the information from the array. Using $.each() and .append() functions.

Comparing and Removing from Arrays in jQuery

Let’s say you don’t want to have Jim in the array anymore, we can use the same $.each() along with the .splice() function to accomplish this.

var removeMe;
$.each( myArray, function(i, v) {
if( v.name == 'Jim' ) {
removeMe = i;
}
});
myArray.splice(removeMe,1);

This is only removing one item, but let’s say you want to compare two arrays and remove all the items in one array from the items in the other array?

We are going to create a function that opens the array outside of the $.each() (I’d like to thank Viral Planet for their JavaScript Array Remove an Element post, helped me create this function with the added .name to allow searching by key name)

function removeByValue(arr, val) {
for(var i=0; i
if(arr[i].name == val) {
arr.splice(i, 1);
break;
}
}
}
var names = ['Jim','Fred'];
$.each( names, function(i, v) {
removeByValue(myArray, v);
});

You can just use this function for removing a single item in the array too, I just wanted to show you a couple different ways. We can also update this function a bit and allow you to add the key name yourself.

function removeByValue(arr, key, val) {
for(var i=0; i
if(arr[i][key] == val) {
arr.splice(i, 1);
break;
}
}
}
var names = ['Jim','Fred'];
$.each( names, function(i, v) {
removeByValue(myArray, 'name', v);
});

Enjoy :D

Comments

  1. says

    I’m familiar with PHP and I really miss its advantages regarding array processing so instead of reinventing the wheel with pure JavaScript I wanted some jQuery guidance. This was it. :) Thanks!

  2. says

    So doing this:

    var myArray = $([]);
    myArray.each( /* .. */ );

    is equivalent to 

    var myArray = [];
    $.each(myArray, /* .. */ );

    right?

  3. says

    Well, that will create an array and allow you to iterate through it, but I don’t believe you are going to get what you want. Just by putting in the $ doesn’t make jQuery know that you want the element as opposed to the text you are typing. You would need to add that during the iteration.

Leave a Reply

Your email address will not be published.