The key difference between pop() and shift() and their cousins push() and unshift(),
is that neither method takes parameters, and each only allows an array to be modified by a single element at a time
what if we want to remove an element from somewhere in the middle? Or remove more than one element at once?
splice() allows us to do just that: remove any number of consecutive elements from anywhere in an array.
splice() can take up to ___ parameters, and the ___ can be used to _____
3, third, add to the array
const numbers = [10, 11, 12, 12, 15]; const startIndex = 3; const amountToDelete = 1;
numbers.splice(startIndex, amountToDelete, 13, 14);
// the second entry of 12 is removed, and we add 13 and 14 at the same index
console.log(numbers);
// returns [ 10, 11, 12, 13, 14, 15 ]
use splice() with 3 parameters for…
quickly switching out an element, or a set of elements, for another.
what slice() does
rather than modifying an array, slice() copies, or extracts, a given number of elements to a new array, leaving the array it is called upon untouched
using slice()
slice() takes 2 arguments: (1) element number to start from, (2) element number to stop at (exclusive)
slice(2,4) will take elements 3 and 4
we can use the spread operator to copy an array like so (code):
let thisArray = [true, true, undefined, false, null]; let thatArray = [...thisArray];
what spread operator can do to string multiple elements from arrays in various combinations (code)
let thisArray = [‘sage’, ‘rosemary’, ‘parsley’, ‘thyme’];
let thatArray = ['basil', 'cilantro', ...thisArray, 'coriander']; // thatArray now equals ['basil', 'cilantro', 'sage', 'rosemary', 'parsley', 'thyme', 'coriander']
Check For The Presence of an Element With
indexof()
fruits. indexOf(‘oranges’); // returns 2
fruits. indexOf(‘pears’); // returns 1, the first index at which the element exists
what indexof() returns if the element doesn’t exist in an array
let fruits = [‘apples’, ‘pears’, ‘oranges’, ‘peaches’, ‘pears’];
fruits.indexOf(‘dates’); // returns -1
remove a key-value pair from an object
delete obj.key;
what if we just wanted to know if an object has a specific property?
two different ways to do this:
hasOwnProperty()
the in keyword
users.hasOwnProperty(‘Alan’);
‘Alan’ in users;
Objects ____ maintain an ordering to stored keys like arrays do; thus a key’s position on an object, or the relative order in which it appears, is ____ when referencing or accessing that key.
do not, irrelevant
Sometimes you may need to iterate through all the keys within an object. This requires a specific syntax in JavaScript called a ____ statement.
for…in
Example of for…in statement (code)
for (let user in users) {
console.log(user);
}
// logs: Alan Jeff Sarah Ryan
generate an array which contains all the keys stored in an object using ____
the Object.keys() method
Object.keys() accepts ____ as argument
an object
Object.keys(obj) will return _____. Again, there will be ______ to the entries in the array.
an array with strings representing each property in the object, no specific order
Object.assign() does…
takes a target object and source objects and maps properties from the source to target
commonly used to make shallow copies of objects
const newObject = Object.assign({}, obj1, obj2);