Arrays and Objects Flashcards
What data type is the following: let colors = ['blue', 'green', 'purple']
Array
What data type is the following: let colors = {'blue', 'green', 'purple'}
Object
What will be console logged? let dogs = ['Husky', 'Shiba Inu', 'Beagle', 'Dalmatian'] console.log( dogs[1] )
Shiba Inu
What index number do arrays start at?
0
Which array method will append something to the end of an array?
.push( )
Example Usage: let dogs = ['Husky', 'Shiba Inu', 'Beagle', 'Dalmatian']; dogs.push('Golden Retriever');
Which array method can be used to cut something out of an array and add something else?
.splice( )
Example Usage: let dogs = ['Husky', 'Shiba Inu', 'Beagle', 'Dalmatian'] dogs.splice(0, 1, 'Corgi') //This will remove Husky and replace it with Corgi
The array method .splice( ) takes in three parameters. What is the purpose of each of these three parameters?
Array.splice(position, how many to cut, what to add in that location)
What array method can be used to remove the last element of an array?
.pop( )
Example Usage: let characters = ['Mario', 'Luigi', 'Yoshi', 'Peach'] characters.pop( ) //This will remove Peach
What array method can be used to remove the first item of an array?
. shift( )
Example Usage: let characters = ['Mario', 'Luigi', 'Yoshi', 'Peach'] characters.shift( ) //This will remove Mario
What will happen in the following code: let characters = ['Mario', 'Luigi', 'Yoshi', 'Peach'] characters.unshift('Bowser', 'Wario')
It will add Bowser and Wario to the beginning of the array
[‘Bowser’, ‘Wario’, ‘Mario’, ‘Luigi’, ‘Yoshi’, ‘Peach’]
What are the three arguments for a forEach( )
- ) currentValue = the current element being processed in the array
- ) index = the index of the current element being processed
- ) array = the array the forEach method was called upon
What is the syntax for a forEach( ) that will console log each of the items in this array? let characters = ['Mario', 'Luigi', 'Yoshi', 'Peach']
characters.forEach(c => { console.log(c); })
What will this console log?
let food = [‘Pizza’, ‘Pasta’, ‘Hamburger’, ‘Chips’, ‘Ice Cream’]
console.log(food.length)
5
How would you step through this object and console log ‘fire’?
let pokemon = { name: 'Bulbasaur', id: 1, type: ['grass', 'poison'], weakness: ['psychic', 'flying', 'fire', 'ice'], evolvesTo: 'Ivysaur', description: 'A strange seed was planted on its back at birth. The plant sprouts and grows with this Pokémon.' }
console.log(pokemon.weakness[2])
What does JSON stand for?
JavaScript Object Notation