Array Flashcards
Get familiar with all the array properties in JavaScript
Method determines whether an array includes a certain element, returning true or false as appropriate.
var array1 = [1, 2, 3];
console.log(array1.includes(2)); // expected output: true
var pets = [‘cat’, ‘dog’, ‘bat’];
console.log(pets.includes('cat')); // expected output: true
console.log(pets.includes('at')); // expected output: false
Method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.
var array1 = [5, 12, 8, 130, 44];
var found = array1.find(function(element) { return element > 10; });
console.log(found); // expected output: 12
Method returns a string representing the elements of the array. The elements are converted to Strings using their toLocaleString methods and these Strings are separated by a locale-specific String (such as a comma “,”).
var array1 = [1, 'a', new Date('21 Dec 1997 14:12:00 UTC')]; var localeString = array1.toLocaleString('en', {timeZone: "UTC"});
console.log(localeString); // expected output: "1,a,12/21/1997, 2:12:00 PM", // This assumes "en" locale and UTC timezone - your results may vary
Method tests whether at least one element in the array passes the test implemented by the provided function.
var array = [1, 2, 3, 4, 5];
var even = function(element) { // checks whether an element is even return element % 2 === 0; };
console.log(array.some(even)); // expected output: true
Method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
var array1 = ['a', 'b', 'c']; var array2 = ['d', 'e', 'f'];
console.log(array1.concat(array2)); // expected output: Array ["a", "b", "c", "d", "e", "f"]
Method returns a new Array Iterator object that contains the keys for each index in the array.
var array1 = ['a', 'b', 'c']; var iterator = array1.keys();
for (let key of iterator) {
console.log(key); // expected output: 0 1 2
}
Method changes the contents of an array by removing existing elements and/or adding new elements.
var months = [‘Jan’, ‘March’, ‘April’, ‘June’];
months.splice(1, 0, ‘Feb’);
// inserts at 1st index position
console.log(months);
// expected output: Array [‘Jan’, ‘Feb’, ‘March’, ‘April’, ‘June’]
months.splice(4, 1, ‘May’);
// replaces 1 element at 4th index
console.log(months);
// expected output: Array [‘Jan’, ‘Feb’, ‘March’, ‘April’, ‘May’]
Method adds one or more elements to the beginning of an array and returns the new length of the array.
var array1 = [1, 2, 3];
console.log(array1.unshift(4, 5)); // expected output: 5
console.log(array1); // expected output: Array [4, 5, 1, 2, 3]
Method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex.
var animals = [‘Dodo’, ‘Tiger’, ‘Penguin’, ‘Dodo’];
console.log(animals.lastIndexOf('Dodo')); // expected output: 3
console.log(animals.lastIndexOf('Tiger')); // expected output: 1
Method removes the last element from an array and returns that element. This method changes the length of the array.
var plants = [‘broccoli’, ‘cauliflower’, ‘cabbage’, ‘kale’, ‘tomato’];
console.log(plants.pop()); // expected output: "tomato"
console.log(plants); // expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]
plants.pop();
console.log(plants); // expected output: Array ["broccoli", "cauliflower", "cabbage"]
Method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating no element passed the test.
var array1 = [5, 12, 8, 130, 44];
function findFirstLargeNumber(element) { return element > 13; }
console.log(array1.findIndex(findFirstLargeNumber)); // expected output: 3
Method returns the first index at which a given element can be found in the array, or -1 if it is not present.
var beasts = [‘ant’, ‘bison’, ‘camel’, ‘duck’, ‘bison’];
console.log(beasts.indexOf('bison')); // expected output: 1
// start from index 2 console.log(beasts.indexOf('bison', 2)); // expected output: 4
console.log(beasts.indexOf('giraffe')); // expected output: -1
Returns the number of elements in that array.
var clothing = [‘shoes’, ‘shirts’, ‘socks’, ‘sweaters’];
console.log(clothing.length); // expected output: 4
Method removes the first element from an array and returns that removed element. This method changes the length of the array.
var array1 = [1, 2, 3];
var firstElement = array1.shift();
console.log(array1); // expected output: Array [2, 3]
console.log(firstElement); // expected output: 1
Method sorts the elements of an array in place and returns the array. Javascript sort algorithm on V8 is now stable. The default sort order is according to string Unicode code points.
The time and space complexity of the sort cannot be guaranteed as it is implementation dependent.
var months = [‘March’, ‘Jan’, ‘Feb’, ‘Dec’];
months.sort();
console.log(months);
// expected output: Array [“Dec”, “Feb”, “Jan”, “March”]
var array1 = [1, 30, 4, 21];
array1.sort();
console.log(array1);
// expected output: Array [1, 21, 30, 4]