Arrays Flashcards
What is an array
An array is an ordered collection of values
How is each value of an array called ?
An element
How is the position of a value in an array called ?
An index
What does it mean to say that javascript arrays are untyped.
An array element may be of any type, and different elements of the same array may be of different types.
What is a sparse array ?
It is an array with gaps. Elements are not contiguous.
What are the different ways to create an array ?
- Use Array Literals
- Use the constructor Array()
- Use Array.of
- Use Array.from
- Use the … spread operator on an iterable object
What is the length of this Array litteral and why ?
[,,]
This array litteral has a length of 2.
The last comma is considered as a trailing one. Not a separating comma.
I which cases Array.of() should be used ?
Array.of() is a factory method that creates and returns a new array, using its argument values (regardless of how many of them there are) as the array elements:
Array.of() // => []; returns empty array with no arguments
Array.of(10) // => [10]; can create arrays with a single numeric argument
Array.of(1,2,3) // => [1, 2, 3]
It is comparable with the Array() constructor called with multiple arguments. The difference is that that Array() constructor cannot be used to create an array with a single numeric value specified
In which cases the length property of an Array is automatically updated ?
- when you assign an element of the array using property names that are non-negative integers less than 232–1.
What is the difference between an array index and an array property name ?
All indexes are property names, but only property names that are integers between 0 and 232–2 are indexes.
What is the particularity of the length property of an array when that array is sparse ?
If the array is sparse, the length property is no more the number of elements in the array. Rather, the value of the length property is greater than the number of elements.
What is the purpose of the method push() of arrays ?
To add one or more values to the end of an array:
let a = []; // Start with an empty array
a.push(“zero”); // Add a value at the end.
a = [“zero”]
a.push(“one”, “two”); // Add two more values.
a = [“zero”, “one”, “two”]
Pushing a value onto an array a is the same as assigning the value to a[a.length]
What is the purpose of the method unshift() of arrays ?
To insert a value at the beginning of an array, shifting the existing array elements to higher indexes.
What is the purpose of the method pop() of arrays ?
The pop() method is the opposite of push(): it removes the last element of the array and returns it, reducing the length of an array by 1.
What is the purpose of the method shift() of arrays ?
shift() method removes and returns the first element of the array, reducing the length by 1 and shifting all elements down to an index one lower than their current index.
What is the difference between the method forEach() of arrays and the loop for/of ?
You pass a function to the forEach() method of an array, and forEach() invokes your function once on each element of the array.
Unlike the for/of loop, the forEach() is aware of sparse arrays and does not invoke your function for elements that are not there.
forEach() does not provide a way to terminate iteration before all elements have been passed to the function. That is, there is no equivalent of the break statement you can use with a regular for loop
What is the particularity of array Iterator methods when the array is sparse ?
If the array is sparse, the function you pass is not invoked for nonexistent elements.
List the 3 arguments the forEach method (as well as some other Iterator methods) receive.
the value of the array element
The index of the array element.
And the array itself.
Describe the map() method of arrays
The map() method passes each element of the array on which it is invoked to the function you specify and returns an array containing the values returned by your function.
For example:
let a = [1, 2, 3];
a.map(x => xx) // => [1, 4, 9]: the function takes input x and returns xx
Which differences exist between forEach() method of arrays and map() method ?
The function you pass to map() is invoked in the same way as a function passed to forEach(). For the map() method, however, the function you pass should return a value.
map() returns a new array: it does not modify the array it is invoked on. forEach() allIf that array is sparse, your function will not be called for the missing elements, but the returned array will be sparse in the same way as the original array: it will have the same length and the same missing elements
Describe the method filter() of arrays
The filter() method returns an array containing a subset of the elements of the array on which it is invoked. The function you pass to it should be predicate: a function that returns true or false.