Javascript: Arrays
An array is a data structure; e.g. a list of data. Think of var but for multiple information/data, e.g. a list of the names of all your friends (not just one friend - Lord forbid you have only one friend?!).
You can use arrays to structure/organize things, e.g. comments, photos, etc.
Objectives for this issue:
- Define an array (using codes), remove and add elements arrays
- Use "push", "pop", "shift", "unshift" --> which are essentially built-in methods
var dogs = ["Rosie", "Max", "Summer", "Hazel"]
console.log(dogs[0]) //"Rosie"
Push and Pop
Push adds another item to end of an array
var writers = ["Joanne Rowling", "David Foster Wallace"]
writers.push("John Williams")
console.log(writers) // "Joanne Rowling", "David Foster Wallace", "John Williams"
Pop takes the last item out, without taking in any argument.
var writers = ["Joanne Rowling", "David Foster Wallace", "John Williams"]
writers.pop()
console.log(writers) // "Joanne Rowling", "David Foster Wallace"
Unshift and Shift
Unshift adds an item to the front of the array, like so:
var whatever = [...]
whatever.unshift ("thing to add at the front of the list")
console.log(whatever) // "thing to add at the front of the list", "..."
Shift removes an item from the front of array, without taking an argument (just like pop)
var Dwarves = ["Happy", "Sleepy", "Grumpy"]
Dwarves.shift()
console.log(Dwarves) // "Sleepy", "Grumpy"
indexOf
Basically this will give you the index of an item on the array
var friends = ["Hagrid", "Dumbledore", "Snape"]
friends.indexOf("Hagrid"); //0
Slice vs Splice
This basically copies whatever you want to copy from an array
var characters = ["Harry Potter", "William Stoner", "Tom Riddle", "Gordon Finch"]
var HarryPotter = characters.slice (0,2) // "Harry Potter", "Tom Riddle"
var AllCharacters = characters.slice ()
Splice is rare - so just google that shit when it comes up on your screen.
REMEMBER: No spacing allowed in var names!
Tadah!
Javascript: Arrays
An array is a data structure; e.g. a list of data. Think of
varbut for multiple information/data, e.g. a list of the names of all your friends (not just one friend - Lord forbid you have only one friend?!).You can use arrays to structure/organize things, e.g. comments, photos, etc.
Objectives for this issue:
Push and Pop
Push adds another item to end of an array
Pop takes the last item out, without taking in any argument.
Unshift and Shift
Unshift adds an item to the front of the array, like so:
Shift removes an item from the front of array, without taking an argument (just like
pop)indexOf
Basically this will give you the index of an item on the array
Slice vs Splice
This basically copies whatever you want to copy from an array
Splice is rare - so just google that shit when it comes up on your screen.
Tadah!