Array Flashcards

Get familiar with all the array properties in JavaScript

1
Q

Method determines whether an array includes a certain element, returning true or false as appropriate.

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Method returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.

A

var array1 = [5, 12, 8, 130, 44];

var found = array1.find(function(element) {
  return element > 10;
});
console.log(found);
// expected output: 12
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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 “,”).

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Method tests whether at least one element in the array passes the test implemented by the provided function.

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

A
var array1 = ['a', 'b', 'c'];
var array2 = ['d', 'e', 'f'];
console.log(array1.concat(array2));
// expected output: Array ["a", "b", "c", "d", "e", "f"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Method returns a new Array Iterator object that contains the keys for each index in the array.

A
var array1 = ['a', 'b', 'c'];
var iterator = array1.keys(); 

for (let key of iterator) {
console.log(key); // expected output: 0 1 2
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Method changes the contents of an array by removing existing elements and/or adding new elements.

A

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’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Method adds one or more elements to the beginning of an array and returns the new length of the array.

A

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]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.

A

var animals = [‘Dodo’, ‘Tiger’, ‘Penguin’, ‘Dodo’];

console.log(animals.lastIndexOf('Dodo'));
// expected output: 3
console.log(animals.lastIndexOf('Tiger'));
// expected output: 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Method removes the last element from an array and returns that element. This method changes the length of the array.

A

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"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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.

A

var array1 = [5, 12, 8, 130, 44];

function findFirstLargeNumber(element) {
  return element > 13;
}
console.log(array1.findIndex(findFirstLargeNumber));
// expected output: 3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Method returns the first index at which a given element can be found in the array, or -1 if it is not present.

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Returns the number of elements in that array.

A

var clothing = [‘shoes’, ‘shirts’, ‘socks’, ‘sweaters’];

console.log(clothing.length);
// expected output: 4
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Method removes the first element from an array and returns that removed element. This method changes the length of the array.

A

var array1 = [1, 2, 3];

var firstElement = array1.shift();

console.log(array1);
// expected output: Array [2, 3]
console.log(firstElement);
// expected output: 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Method fills all the elements of an array from a start index to an end index with a static value. The end index is not included.

A

var array1 = [1, 2, 3, 4];

// fill with 0 from position 2 until position 4
console.log(array1.fill(0, 2, 4));
// expected output: [1, 2, 0, 0]
// fill with 5 from position 1
console.log(array1.fill(5, 1));
// expected output: [1, 5, 5, 5]
console.log(array1.fill(6));
// expected output: [6, 6, 6, 6]
17
Q

Method applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.

A

const array1 = [[0, 1], [2, 3], [4, 5]].reduceRight(
(previousValue, currentValue) => previousValue.concat(currentValue)
);

console.log(array1);
// expected output: Array [4, 5, 2, 3, 0, 1]
18
Q

Method executes a reducer function (that you provide) on each member of the array resulting in a single output value.

A
const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10
// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15
19
Q

Method returns a new Array Iterator object that contains the values for each index in the array.

A
const array1 = ['a', 'b', 'c'];
const iterator = array1.values();

for (const value of iterator) {
console.log(value); // expected output: “a” “b” “c”
}

20
Q

Method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string.

A

var elements = [‘Fire’, ‘Wind’, ‘Rain’];

console.log(elements.join());
// expected output: Fire,Wind,Rain
console.log(elements.join(''));
// expected output: FireWindRain
console.log(elements.join('-'));
// expected output: Fire-Wind-Rain
21
Q

Method creates a new, shallow-copied Array instance from an array-like or iterable object.

A
console.log(Array.from('foo'));
// expected output: Array ["f", "o", "o"]
console.log(Array.from([1, 2, 3], x => x + x));
// expected output: Array [2, 4, 6]
22
Q

Method tests whether all elements in the array pass the test implemented by the provided function.

A
function isBelowThreshold(currentValue) {
  return currentValue < 40;
}

var array1 = [1, 30, 39, 29, 10, 13];

console.log(array1.every(isBelowThreshold));
// expected output: true
23
Q

Method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified.

A

var animals = [‘ant’, ‘bison’, ‘camel’, ‘duck’, ‘elephant’];

console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]
console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]
24
Q

Method returns a string representing the source code of the array.

A

var alpha = new Array(‘a’, ‘b’, ‘c’);

alpha.toSource();
//returns ['a', 'b', 'c']
25
Q

Method executes a provided function once for each array element.

A

var array1 = [‘a’, ‘b’, ‘c’];

array1.forEach(function(element) {
console.log(element);
});

// expected output: "a"
// expected output: "b"
// expected output: "c"
26
Q

Method adds one or more elements to the end of an array and returns the new length of the array.

A

var animals = [‘pigs’, ‘goats’, ‘sheep’];

console.log(animals.push('cows'));
// expected output: 4
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]

animals.push(‘chickens’);

console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows", "chickens"]
27
Q

Method returns a new Array Iterator object that contains the key/value pairs for each index in the array.

A

var array1 = [‘a’, ‘b’, ‘c’];

var iterator1 = array1.entries();

console.log(iterator1.next().value);
// expected output: Array [0, "a"]
console.log(iterator1.next().value);
// expected output: Array [1, "b"]
28
Q

Method creates a new Array instance with a variable number of arguments, regardless of number or type of the arguments.

A

Array.of(7); // [7]
Array.of(1, 2, 3); // [1, 2, 3]

Array(7); // [ , , , , , , ]
Array(1, 2, 3); // [1, 2, 3]

29
Q

Method determines whether the passed value is an Array.

A

Array.isArray([1, 2, 3]); // true
Array.isArray({foo: 123}); // false
Array.isArray(‘foobar’); // false
Array.isArray(undefined); // false

30
Q

Method shallow copies part of an array to another location in the same array and returns it, without modifying its size.

A

var array1 = [1, 2, 3, 4, 5];

// place at position 0 the element between position 3 and 4
console.log(array1.copyWithin(0, 3, 4));
// expected output: Array [4, 2, 3, 4, 5]
// place at position 1 the elements after position 3
console.log(array1.copyWithin(1, 3));
// expected output: Array [4, 4, 5, 4, 5]
31
Q

Method returns a string representing the specified array and its elements.

A

var array1 = [1, 2, ‘a’, ‘1a’];

console.log(array1.toString());
// expected output: "1,2,a,1a"
32
Q

Method creates a new array with the results of calling a provided function on every element in the calling array.

A

var array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);
console.log(map1);
// expected output: Array [2, 8, 18, 32]
33
Q

Method reverses an array in place. The first array element becomes the last, and the last array element becomes the first.

A
var array1 = ['one', 'two', 'three'];
var reversed = array1.reverse(); 
console.log(array1);
// expected output: Array ['three', 'two', 'one']
console.log(reversed);
// expected output: Array ['three', 'two', 'one']
34
Q

Method creates a new array with all elements that pass the test implemented by the provided function.

A

var words = [‘spray’, ‘limit’, ‘elite’, ‘exuberant’, ‘destruction’, ‘present’];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]