array methods © copyright 2014, fred mcclurg all rights reserved
TRANSCRIPT
![Page 1: Array Methods © Copyright 2014, Fred McClurg All Rights Reserved](https://reader035.vdocument.in/reader035/viewer/2022072016/56649ede5503460f94beeb13/html5/thumbnails/1.jpg)
Array Methods
© Copyright 2014, Fred McClurg All Rights Reserved
![Page 2: Array Methods © Copyright 2014, Fred McClurg All Rights Reserved](https://reader035.vdocument.in/reader035/viewer/2022072016/56649ede5503460f94beeb13/html5/thumbnails/2.jpg)
2
Manipulating Arrays
Examples:
var city = [ "Santa Fe", "Cedar Rapids" ];
// overwrite first elementcity[0] = "Des Moines";
// append element to array endcity[city.length] = "Iowa City";
// display entire arrayconsole.log( city );
manipArray.html
![Page 3: Array Methods © Copyright 2014, Fred McClurg All Rights Reserved](https://reader035.vdocument.in/reader035/viewer/2022072016/56649ede5503460f94beeb13/html5/thumbnails/3.jpg)
3
Array Method: pop()
Discussion:
Array method “pop()” removes the last element from the array.
Example:
var marx = [ "Chico", "Harpo", "Groucho", "Gummo", "Zeppo" ];
console.log( "Array before pop: " + marx );
// remove last element from arrayvar youngest = marx.pop(); // "Zeppo"
console.log( "Returned from pop(): " + youngest );console.log( "Array after pop(): " + marx );arrayPop.html
![Page 4: Array Methods © Copyright 2014, Fred McClurg All Rights Reserved](https://reader035.vdocument.in/reader035/viewer/2022072016/56649ede5503460f94beeb13/html5/thumbnails/4.jpg)
4
Array Method: push()
Discussion:
The method “push()” appends an element to the end of an array.
Example:
var marx = [ "Chico", "Harpo" ];console.log( "Original array: " + marx );
// append element to array endmarx.push( "Grocho" );console.log( "After push(): " + marx );
arrayPush.html
![Page 5: Array Methods © Copyright 2014, Fred McClurg All Rights Reserved](https://reader035.vdocument.in/reader035/viewer/2022072016/56649ede5503460f94beeb13/html5/thumbnails/5.jpg)
5
Discussion:
The method “shift()” removes the first element from an array.
Example:
var bears = [ "Goldilocks", "Mama", "Papa" ];console.log( "Original array: " + bears );
// remove first element from arrayvar girl = bears.shift(); // "Goldilocks"
console.log( "Returned from shift(): " + girl );console.log( "Array after shift(): " + bears );
Array Method: shift()
arrayShift.html
![Page 6: Array Methods © Copyright 2014, Fred McClurg All Rights Reserved](https://reader035.vdocument.in/reader035/viewer/2022072016/56649ede5503460f94beeb13/html5/thumbnails/6.jpg)
6
Array Method: unshift()
Discussion:
The method “unshift()” adds an element to the beginning of the array.
Example:var bears = [ "Mama", "Papa" ];console.log( "Original array: " + bears );
// insert first element into arraybears.unshift( "Baby" );console.log( "After unshift(): " + bears );
arrayUnshift.html
![Page 7: Array Methods © Copyright 2014, Fred McClurg All Rights Reserved](https://reader035.vdocument.in/reader035/viewer/2022072016/56649ede5503460f94beeb13/html5/thumbnails/7.jpg)
7
Array Method: splice()
Discussion:
This method adds or removes elements from an array.
Example:
var taylor = [ "Andy", "Barney", "Gomer", "Aunt Bee", "Opie", ];
console.log( "Original array: " + taylor );
// starting at taylor[1] remove two elementsdeputy = taylor.splice( 1, 2 );
console.log( "Array after splice(1, 2): " + taylor );console.log( "Returned by splice(1, 2): " + deputy );
arraySplice.html
![Page 8: Array Methods © Copyright 2014, Fred McClurg All Rights Reserved](https://reader035.vdocument.in/reader035/viewer/2022072016/56649ede5503460f94beeb13/html5/thumbnails/8.jpg)
8
Array Method: slice()
Discussion:
Selects elements from an the array but does not modify the original array.
Example:
var hecks = [ "Mike", "Frankie", "Axl", "Sue", "Brick" ];
// copy "Axl" and "Sue"var oldestKids = hecks.slice(2, 4);
console.log( "hecks: " + hecks );console.log( "oldestKids: " + oldestKids );arraySort.html
![Page 9: Array Methods © Copyright 2014, Fred McClurg All Rights Reserved](https://reader035.vdocument.in/reader035/viewer/2022072016/56649ede5503460f94beeb13/html5/thumbnails/9.jpg)
9
Array Method: sort()
Discussion:
This method sorts the elements of the array.
Example:
var dwarfs = [ "Doc", "Grumpy", "Happy", "Sleepy", "Bashful", "Sneezy", "Dopey" ];
console.log( "Original array: " + dwarfs );
dwarfs.sort();console.log( "Array after sort(): " + dwarfs );
arraySort.html
![Page 10: Array Methods © Copyright 2014, Fred McClurg All Rights Reserved](https://reader035.vdocument.in/reader035/viewer/2022072016/56649ede5503460f94beeb13/html5/thumbnails/10.jpg)
14
Discussion:
Split breaks an string into array elements. Reverse changes the array order. Join combines array elements back into a string.
Example (convert string to backwards):
var word = "stressed";console.log( "Original word: " + word );
var letters = word.split( "" ); // convert string to arrayconsole.log( "Array after split(): " + letters );
letters.reverse(); // reverse array orderconsole.log( "Array after reverse(): " + letters );
var backward = letters.join( "" ); // convert array to strconsole.log( "Word after join(): " + backward );
Array Methods: split(), reverse(), join()
arraySplitRevJoin.html