ng-workshop: Arrays and Objects
by junak
JavaScript
//Arrays
var coffees = ['cortado', 'latte', 'espresso'];
var latte = coffees[1]; // latte
console.log(coffees.pop()); //espresso
var lighterCoffees = coffees.slice(0, 2); // ['cortado', 'latte']
coffees.push('cappuccino');
console.log(coffees); //['cortado', 'latte', 'cappuccino']
//Objects
var goodMovies = [];
goodMovies.push({
title: "The Matrix",
year: 1999
});
goodMovies.push({
title: "Dark Knight Rises",
year: 2012
});
console.log(goodMovies[1]); //Object {title: "Dark Knight Rises", year: 2012}
var theOne = {
name: "Neo",
"mentor": "Morpheus",
isThereASpoon: function(){
return false;
},
who: function(){
return this.name;
}
};
//“The Bourne Identity” at the start,
var bourneFilms=['The Bourne Supremacy','The Bourne Ultimatum','The Bourne Legacy'];
function addAtStart(arr,val){
arr.unshift(val);
}
addAtStart(bourneFilms,“The Bourne Identity”);
console.log(bourneFilms);
console.log(theOne.isThereASpoon());
console.log(theOne.who());