23.04.2013 Views

javascript

javascript

javascript

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.

Figure 6–39. Deleting and inserting elements into an array with splice()<br />

CHAPTER 6 ■ FUNCTIONS AND ARRAYS<br />

Now, say the 2000–2002 seasons are simply missing. We can use splice() to insert those. That is to<br />

say, splice() does not require you to delete any elements. So if we pass 0 for the second parameter to<br />

splice(), JavaScript will insert the three new elements, and, as before, keep the indexes sequential<br />

behind the scenes, as Figure 6–40 displays.<br />

var pirates = [[2010, 57, 105],<br />

[2009, 62, 99],<br />

[2008, 67, 95],<br />

[2007, 68, 94],<br />

[2006, 67, 95],<br />

[2005, 67, 95],<br />

[2004, 72, 89],<br />

[2003, 75, 87],<br />

[1999, 78, 83],<br />

[1998, 69, 93],<br />

[1997, 79, 83],<br />

[1996, 73, 89],<br />

[1995, 58, 86],<br />

[1994, 53, 61],<br />

[1993, 75, 87]];<br />

pirates.splice(8, 0, [2002, 72, 89], [2001, 62, 100], [2000, 69, 93]);<br />

console.dir(pirates);<br />

251

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

Saved successfully!

Ooh no, something went wrong!