Friday, July 26, 2013

Array Methods

Concatenate Arrays



                                                                             
<script>
var design = new Array("Typograhpy", "Photoshop", "Illustator");
var development = new Array("HTML5", "CSS3", "PHP", "MySQL", "JavaScript");
var programming = new Array(".NET", "MS SQL", "Java");

var courses = design.concat(development, programming);


document.write(courses[10]);

</script>


Join and Pop

<script>
var design = new Array("Typograhpy",           "Photoshop", "Illustator");
var development = new Array("HTML5", "CSS3", "PHP", "MySQL", "JavaScript");
var programming = new Array(".NET", "MS SQL", "Java", "undefined");
        //3 arrays
programming.pop();//This will remove the last index from the 3rd Array 
document.write(programming[4] + "<br>");//I am undefined
//Now add both the 2nd and 3rd Arrays to the 1st Array and store in a new Array
var courses = design.concat(development, programming);

//////////////////////////////////////////////////////////////////////////////////////////////////////
var join1 = courses.join(", ");//join, and add comma - space between indices
document.write(join1 + "<br>");
        //////////////////////////////////////////////////////////////////////////////////////////////////////

</script>

Instead of writing these last two lines, you can simply write:
         //////////////////////////////////////////////////////////
         document.write(courses.join(", "));
         //////////////////////////////////////////////////////////

This next method should be called shove, because it shoves new elements into your Array:
        courses.push("Flash", "jQuery");

List alphabetically:
     courses.sort();

Reverse it first, then join echo:
        courses.reverse();

document.write(courses.join(", "));

No comments:

Post a Comment