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)
to deteremine if a value is an array. Returns true or false.
when to use? Essential for cases where you have mixed data types and need to distinguish between arrays and other objects.
array.isArray()
let numbers = [1,2,3,4,5]
console.log(Array.isArray(numbers)); // true (it’s an array)
console.log(Array.isArray([1, 2, 3])); // true (it’s an array)
console.log(Array.isArray(“Hello”)); // false (it’s a string)
console.log(Array.isArray({a: 1})); // false (it’s an object)
console.log(Array.isArray(123)); // false (it’s a number)
working with nested arrays.
let arr = [1, [2, 3], “Hello”];
cosnole.log(Array.isArray(arr[0)); false (bacause 1 is a number)
console.log(Array.isArray(arr[1])); // true (because [2, 3] is an array)
console.log(Array.isArray(arr[2])); // false (because “Hello” is a string)
I can sort mixedData with it if I use loops and conditions:
let mixedData = [1, “Hello”, [2, 3], true];
let newArr = [];
for (let item of mixedData) {
if (Array.isArray(item)) {
console.log(“This is an array:”, item);
newArr.push(item)
} else {
console.log(“This is not an array:”, item);
}
};
is the position of an element inside an array
index
var fruits = [“banana”,”apple”,”peach”]
console.log(fruits[1]) // “apple”
add and replace /overwrite via Index var fruits = [] fruits[0] = “apple” fruits[1] = “peach” fruits[2] = “banana” console.log(fruits) // [“apple”,”peach”,”banana”] fruits[2] = “kiwi” console.log(fruits) // [“apple”,”peach”,”kiwi”]
tells us how many elements
length
var data = [“zero”, “one”, “two”]
console.log(data.length) // 3
we can control the length and cut out the rest var data = [“zero”, “one”, “two”, “three”, “four”] data.length = 2 console.log(data.length) // 2
unpacking an array based on the Index of the elements inside of the array
destructuring
Refers to the syntax for extracting values from arrays or properties from objects into distinct variables.
const people = [“Peter”,”Robert”,”Jenny”] const [ potato, car] = people console.log(car) // “Robert” console.log(potato) // “Peter”
creates a new array with the same elements as the original array. This ensures the new array is independent of the original. Has 3 more important features.
3 ways to use it
(…) shallow Copying, combining (merging), rest syntax (Reverse of spreading)
Copying:
let original = [10, 2, 5];
let copy = […original]; // Creates a shallow copy
console.log(copy); // [10, 2, 5]
console.log(original === copy); // false (they are not the same array in memory)
Combining (merging):
const arr1 = [1, 2];
const arr2 = [3, 4];
const mergedArray = […arr1, …arr2]; // [1, 2, 3, 4]
const obj1 = { a: 1 };
const obj2 = { b: 2 };
const mergedObject = { …obj1, …obj2 }; // { a: 1, b: 2 }
rest syntax (reverse of spreading)
const [a, …rest] = [1, 2, 3]; // a = 1, rest = [2, 3]