collections Flashcards
What are collections?
collections are made up of individual elements. In JS this includes arrays, objects, and kinda strings
Calling theslicemethod without any arguments will return…
a copy of the original string
The first argument of slice specifies…
the index at which to start the extraction
The second argument of slice specifies…
the index where you want to end the extraction. The character at the ending index isn’t part of the returned substring.
sliceandsubstringdiffer…
- When the start index is greater than the end index,
substring
swaps the two arguments whileslice
returns an empty string: - When either argument is negative,
substring
treats them as0
, while, as we saw above,slice
treats them aslength + index
: - We recommend using
String.prototype.slice
When given negative numbers as the indices,slicetreats them as
string length + index. IE, it counts backward from the last char (-1 is the last char)
String.prototype.slicereturns…
a new string
Array.prototype.slicereturns…
a new array
Callingarr.slicewithout any arguments returns…
a shallow copy of the original array
Referencing an out-of-bounds index with slice returns…
undefined
Object keys are also called…
properties
Accessing an index less than0on an array or a string returns…
undefined
to check if that property exists and simply holds undefined, or does not exist, we can use…
let obj = { a: ‘foo’, b: ‘bar’, c: undefined};
Object.keys(obj).includes(‘c’); // => true
//OR
obj.hasOwnProperty(‘c’); // => true
obj.hasOwnProperty(‘d’); // => false
Using a key to access a property that doesn’t exist on an object returns…
undefined
We can access just the keys or just the values from an object with
theObject.keysandObject.valuesmethods. These methods return an array
let capitals = { uk: ‘London’, france: ‘Paris’, germany: ‘Berlin’ };
Object.keys(capitals); // => [‘uk’, ‘france’, ‘germany’]
Object.values(capitals); // => [‘London’, ‘Paris’, ‘Berlin’]
//Whats happening here?
Object.values(capitals)[0];
// => ‘London’
We are making an array element reference because object.values returns an array
JavaScript arrays are objects. The chief difference between an array and some other object is that…
it uses non-negative integers as its primary keys.
What happens to an array when you add elements?
the array increases the value of itslengthproperty
changing the value of thelengthproperty on an array causes…
the number of elements to change.
What happens if you add add new properties to an array just as you would any other object
these properties are NOT elements of the array but ordinary properties and these properties will not be counted in arr.length. Those properties are are also ignored by array methods likeforEach,map, andfilter
if you have empty elements from manually lengthening an array, iterative array methods will…
skip over these
when we use anObjectmethod, such askeys on an array with non element properties…
we get a list of all of the property names
when we use anObjectmethod, such askeys on an array with unset elements…
keys ignores the unset elements
String.prototype.split, when called without any arguments, returns…
an array with the string as its only element
When called without any arguments,Array.prototype.joinreturns…
a string with the elements of the array joined together into a string, separated by commas.
Apart from theObject.keysandObject.valuesmethods, you can use_______________ to convert an object to an array.
Object.entries
Can you can increment an already set array value?
Yes,
let numbers = [1, 2, 3, 4];
numbers[0] = numbers[0] + 1; // => 2
numbers; // => [ 2, 2, 3, 4 ]
What happens if you increment an unset array index?
you end up with a NaN in that element
string element reassignment…
is syntactically permitted, but doesn’t affect the string