Array Flashcards
Modifies the original array by adding or removing elements at a specific index.
array.splice()
Syntax: array.splice(start, deleteCount, …itemsToAdd)
let arr = [1,2,3,4,5];
arr.splice(1,2, ‘a’, ‘b’); // Removes 2 elements starting at index 1 and add ‘a’, ‘b’
console.log(arr) // [1, ‘a’, ‘b’, 4]
also possible to store the spliced elements in new arr. let arr = [1,2,3,4,5]; let cutOut = arr.splice(1,2) console.log(cutOut) // [2,3] console.log(arr) // [1,4,5]
Returns a new array, leaving the original array unchanged, by adding or removing elements.
arra.toSpliced()
Syntax: array.toSpliced(start, deleteCount, …itemsToAdd)
let arr = [1,2,3,4,5];
let newArr = arr.toSpliced(1,2, ‘a’, ‘b’);
console.log(arr); // [1,2,3,4,5] (unchanged)
cosole.log(newArr); [1, ‘a’, ‘b’, 4, 5]
Adds one or more elements to the end of the array and returns the new length.
array.push()
let arr = [1, 2, 3];
arr.push(4);
console.log(arr); // [1, 2, 3, 4]
Removes the last element from an array and returns it.
array.pop()
Let arr = [1,2,3,4,5];
let last = arr.pop();
console.log(arr); // [1,2]
console.log(last); // 3
Removes the first element from an array and returns it.
array.shift()
let arr = [1, 2, 3];
let first = arr.shift();
console.log(arr); // [2, 3]
console.log(first); // 1
Adds one or more lements to the beginning of the array and returns the new length.
array.unshift()
let arr = [2, 3];
arr.unshift(1);
returns 3
console.log(arr); // [1, 2, 3]
let arr = [3, 4, 5];
arr.unshift(0, 1, 2); // Adds 0, 1, and 2 to the start of the array
returns 6
console.log(arr); // [0, 1, 2, 3, 4, 5]
Returns a new array by merging the original array with one or more arrays or values.
array.concat()
let arr1 = [1,2];
let arr2 = [3,4];
let result = arr1.concat(arr2);
console.log(result); // [1, 2, 3, 4]
Returns the first index at which a given element is found, or -1 if not found.
array.indexOf()
let arr = [1, 2, 3];
console.log(arr.indexOf(2)); // 1
console.log(arr.indexOf(4)); // -1
Checks if an array contains a certain value, returning true or false.
array.includes()
let arr = [1, 2, 3];
console.log(arr.includes(2)); // true
console.log(arr.includes(4)); // false
Returns the element at a specified index, supporting negative indices for counting from the end.
array.at()
let arr = [1, 2, 3];
console.log(arr.at(1)); // 2
console.log(arr.at(-1)); // 3
let numbers = [10, 20, 30, 40, 50];
let lastValue = numbers.at(-1); // Gets the last element
console.log(lastValue); // 50
Sorts the elements of the array in place and returns the original array in a sorted way. Default is ascending, also descending and also alphabetical for strings)
3
array.sort()
let arr = [3, 1, 2];
arr.sort();
console.log(arr); // [1, 2, 3]
I can modify the way of sorting it… this is ascending (numbers)
let numbers = [10, 2, 5];
numbers.sort((a, b) => a - b); // Compares numbers directly
console.log(numbers); // [2, 5, 10]
I can modify the way of sorting it… this is descending (numbers)
let numbers = [10, 2, 5];
numbers.sort((a, b) => b - a); // Reverse comparison
console.log(numbers); // [10, 5, 2]
sorting it alphabetically
let fruits = [‘banana’, ‘apple’, ‘cherry’];
fruits.sort(); // Default sort (alphabetical for strings)
console.log(fruits); // [‘apple’, ‘banana’, ‘cherry’]
If I don’t want to modify the original array I can make a copy first with the spread operator(…) to create a shallow copy.
let original = [10, 2, 5];
let sorted = […original].sort((a, b) => a - b);
console.log(original); // [10, 2, 5] (unchanged)
console.log(sorted); // [2, 5, 10]
Reverses the order of elements in the original array.
array.reverse()
let arr = [1, 2, 3];
arr.reverse();
console.log(arr); // [3, 2, 1]
Returns a new array that is a reversed copy of the original.
array.toReversed()
let arr = [1, 2, 3];
let reversed = arr.toReversed();
console.log(arr); // [1, 2, 3] (unchanged)
console.log(reversed); // [3, 2, 1]
Joins all elements of an arrary into a single string. Default seperator is comma . It has the optional separators or no seperator.
2
array.join()
let arr = [1, 2, 3];
console.log(arr.join(‘-‘)); // “1-2-3”
console.log(arr.join(‘ | ‘)); // “1 | 2 | 3”
console.log(arr.join(‘’)); // “123” (no separator)
Used to create a new array by flattening nested arrays up tot a specific depth. Default si one level deep.
When to use? - simplify working with nested arrays. - to clean arrays with empty slots or undefined values. - when processing arrays that result in nested structures (for example map())
array.flat()
Synatx: array.flat(depth);
default depth is 1;
let arr = [1, [2, 3], [4, [5, 6]]];
let flattened = arr.flat();
console.log(flattened); // [1, 2, 3, 4, [5, 6]]
I can customize the depth;
let arr = [1, [2, [3, [4]]]];
console.log(arr.flat(2)); // [1, 2, 3, [4]]
console.log(arr.flat(Infinity)); // [1, 2, 3, 4] (fully flattened)