08.01.2015 Views

Beginning Web Development, Silverlight, and ASP.NET AJAX

Beginning Web Development, Silverlight, and ASP.NET AJAX

Beginning Web Development, Silverlight, and ASP.NET AJAX

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 14 ■ JAVASCRIPT PROGRAMMING WITH <strong>ASP</strong>.<strong>NET</strong> <strong>AJAX</strong> 347<br />

var a = ['Item 1', 'Item 2', 'Item 3', 'Item 4'];<br />

var b = Array.indexOf(a,'Item 2')<br />

// returns '1'<br />

Inserting an Item into an Array<br />

Earlier, you saw the add comm<strong>and</strong>, which adds an item at the end of the array. If you want<br />

to add an item at a specific location within the array, you can use the insert function. It<br />

takes three parameters:<br />

Array: The array that you are inserting the item into<br />

Index: The location where you want to insert it (zero-based)<br />

Item: The item to insert<br />

var a = ['Item 1', 'Item 2', 'Item 3', 'Item 4'];<br />

Array.insert(a,1,'Item 1a');<br />

// Results: 'Item 1', 'Item 1a', 'Item 2', 'Item 3', 'Item 4'<br />

Removing an Item from an Array<br />

There are two methods for removing items from an array. The remove method takes an<br />

item as a parameter <strong>and</strong> removes the first instance of that item from the array, returning<br />

true when successful <strong>and</strong> false otherwise.<br />

var a = ['Item 1', 'Item 2', 'Item 3', 'Item 4'];<br />

Array.remove(a,'Item 2');<br />

// will remove 'Item 2' from the array<br />

The removeAt function removes the item at the specified index from the array.<br />

var a = ['Item 1', 'Item 2', 'Item 3', 'Item 4'];<br />

Array.removeAt(a,1);<br />

// will also remove 'Item 2' from the array

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!