Array Flashcards
const array = [1, 2, 3]
console.log(array.length)
// expected output: 3
const array1 = [5, 12, 8, 130, 44];
let index = 2;
console.log(Using an index of ${index} the item returned is ${array1.at(index)}
);
index = -2;
console.log(Using an index of ${index} item returned is ${array1.at(index)}
);
// expected output: “Using an index of 2 the item returned is 8”
// expected output: “Using an index of -2 item returned is 130”
const a1 = [1, 2, 3] const a2 = [4, 5, 6] const a3 = [7, 8, 9]
const a4 = a1.concat(a2, a3)
console.log(a4)
immutable
// [1,2,3,4,5,6,7,8,9]
const array1 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’];
console. log(array1.copyWithin(0, 3, 4));
console. log(array1.copyWithin(1, 3));
// copy to index 0 the element at index 3
// expected output: Array [“d”, “b”, “c”, “d”, “e”]
// copy to index 1 all elements from index 3 to the end
// expected output: Array [“d”, “d”, “e”, “d”, “e”]
const array1 = [‘a’, ‘b’, ‘c’];
const iterator1 = array1.entries();
console. log(iterator1.next().value);
console. log(iterator1.next().value);
// expected output: Array [0, "a"] // expected output: Array [1, "b"]
does not mutate
var a = ['a', 'b', 'c']; var iterator = a.entries(); for (let e of iterator1) { console.log(e); }
// [0, 'a'] // [1, 'b'] // [2, 'c']
const isBelowThreshold = (currentValue) => currentValue < 40;
const array1 = [1, 30, 39, 29, 10, 13];
console.log(array1.every(isBelowThreshold));
// expected output: true
const array1 = [1, 2, 3, 4];
console. log(array1.fill(0, 2, 4));
console. log(array1.fill(5, 1));
console. log(array1.fill(6));
// fill with 0 from position 2 until position 4 // expected output: [1, 2, 0, 0]
// fill with 5 from position 1 // expected output: [1, 5, 5, 5]
// expected output: [6, 6, 6, 6]
const words = [‘spray’, ‘limit’, ‘elite’, ‘exuberant’, ‘destruction’, ‘present’];
const result = words.filter(word => word.length > 6);
console.log(result);
// expected output: Array [“exuberant”, “destruction”, “present”]
// cb (item, index, array)
// accepts this
arg
const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element > 10);
console.log(found);
// expected output: 12
// cb(item, index, array)
// accepts this
arg
const array1 = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => element > 13;
console.log(array1.findIndex(isLargeNumber));
// expected output: 3
// cb(item, index, array)
// accepts this
arg
// Arrow function findIndex((element) =\> { ... } ) findIndex((element, index) =\> { ... } ) findIndex((element, index, array) =\> { ... } )
// Callback function
findIndex(callbackFn)
findIndex(callbackFn, thisArg)
// Inline callback function findIndex(function callbackFn(element) { ... }) findIndex(function callbackFn(element, index) { ... }) findIndex(function callbackFn(element, index, array){ ... }) findIndex(function callbackFn(element, index, array) { ... }, thisArg)
const arr1 = [0, 1, 2, [3, 4]];
console.log(arr1.flat());
const arr2 = [0, 1, 2, [[[3, 4]]]];
console.log(arr2.flat(2))
// expected output: [0, 1, 2, 3, 4]
// expected output: [0, 1, 2, [3, 4]]
// depth defaults to 1
let arr1 = [1, 2, 3, 4]; arr1.map(x =\> [x \* 2]);
arr1. flatMap(x => [x * 2]);
arr1. flatMap(x => [[x * 2]]);
// [[2], [4], [6], [8]]
// [2, 4, 6, 8]
// only one level is flattened
// [[2], [4], [6], [8]]
like calling map followed by .flat(1)
const array1 = [‘a’, ‘b’, ‘c’];
array1.forEach(element => console.log(element));
// expected output: "a" // expected output: "b" // expected output: "c"
console. log(Array.from(‘foo’));
console. log(Array.from([1, 2, 3], x => x + x));
// expected output: Array [“f”, “o”, “o”]
// expected output: Array [2, 4, 6]
accepts and array-like or iterable
inline mapping
Array.from(arrayLike, mapFn)
const array1 = [1, 2, 3];
console.log(array1.includes(2));
const pets = [‘cat’, ‘dog’, ‘bat’];
console. log(pets.includes(‘cat’));
console. log(pets.includes(‘at’));
// expected output: true
// expected output: true
// expected output: false
accepts a second argument fromIndex
const beasts = [‘ant’, ‘bison’, ‘camel’, ‘duck’, ‘bison’];
console. log(beasts.indexOf(‘bison’));
console. log(beasts.indexOf(‘bison’, 2));
console. log(beasts.indexOf(‘giraffe’));
// expected output: 1
// start from index 2 // expected output: 4 // expected output: -1
Array.isArray([1, 2, 3]);
Array.isArray({foo: 123});
Array.isArray(‘foobar’);
Array.isArray(undefined);
// true // false // false // false
const elements = [‘Fire’, ‘Air’, ‘Water’];
console. log(elements.join());
console. log(elements.join(‘’));
console. log(elements.join(‘-‘));
// expected output: “Fire,Air,Water”
// expected output: “FireAirWater”
// expected output: “Fire-Air-Water”
const array1 = ['a', 'b', 'c']; const iterator = array1.keys();
for (const key of iterator) {
console.log(key);
}
// expected output: 0 // expected output: 1 // expected output: 2
const animals = [‘Dodo’, ‘Tiger’, ‘Penguin’, ‘Dodo’];
console. log(animals.lastIndexOf(‘Dodo’));
console. log(animals.lastIndexOf(‘Tiger’));
console. log(animals.lastIndexOf(‘Tiger’, 2));
// expected output: 3
// expected output: 1
// expected output: -1
const 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]
Array.of(7);
Array(7);
Array.of(1, 2, 3);
Array(1, 2, 3);
// [7] // array of 7 empty slots // [1, 2, 3] // [1, 2, 3]
Like array but fixes the since integer argument issue
const plants = [‘broccoli’, ‘cauliflower’, ‘cabbage’, ‘kale’, ‘tomato’];
console. log(plants.pop());
console. log(plants);
plants. pop();
console. log(plants);
// expected output: “tomato”
// expected output: Array [“broccoli”, “cauliflower”, “cabbage”, “kale”]
// expected output: Array [“broccoli”, “cauliflower”, “cabbage”]
const animals = [‘pigs’, ‘goats’, ‘sheep’];
const count = animals.push('cows'); console.log(count);
console.log(animals);
animals. push(‘chickens’, ‘cats’, ‘dogs’);
console. log(animals);
// expected output: 4
// expected output: Array [“pigs”, “goats”, “sheep”, “cows”]
// expected output: Array [“pigs”, “goats”, “sheep”, “cows”, “chickens”, “cats”, “dogs”]