Arrays are one of the most commonly used data structures in JavaScript. Knowing how to manipulate them effectively is crucial for any JavaScript developer. Today, we are going to explore some useful array methods using a fun example data set — a list of cat breeds. Let’s get started!
Data Setup
Let’s initialize our array with different cat breeds:
const catBreeds = ['Siamese', 'Persian', 'Maine Coon', 'Sphynx', 'Bengal'];
every()
: The All-Checker
The every()
method checks if all elements in an array pass a test (provided as a function) and returns a Boolean value.
const isAllBreedsShort = catBreeds.every(breed => breed.length < 10);
console.log(isAllBreedsShort);
// Output: false, because "Maine Coon" has 10 characters
Original array: ['Siamese', 'Persian', 'Maine Coon', 'Sphynx', 'Bengal'];
some()
: The Any-Checker
The some()
method tests whether at least one element in the array passes the test implemented by the provided function.
const hasShortBreed = catBreeds.some(breed => breed.length < 7);
console.log(hasShortBreed);
// Output: true, because "Sphynx" has only 6 characters
Original array: ['Siamese', 'Persian', 'Maine Coon', 'Sphynx', 'Bengal'];
copyWithin()
: The Copier
The copyWithin()
method shallow-copies part of an array to another location in the same array and returns it without modifying its length
const copiedBreeds = catBreeds.copyWithin(0, 3, 4);
console.log(copiedBreeds);
// Output: ["Sphynx", "Persian", "Maine Coon", "Sphynx", "Bengal"]
Original array: ['Siamese', 'Persian', 'Maine Coon', 'Sphynx', 'Bengal'];
valueOf()
: Self-Returner
The valueOf()
method returns the array itself. It’s used more internally and often doesn’t need to be called explicitly
console.log(catBreeds.valueOf() === catBreeds);
// Output: true
Original array: ['Siamese', 'Persian', 'Maine Coon', 'Sphynx', 'Bengal'];
forEach()
: The Looper
The forEach()
method executes a function once for each array element.
catBreeds.forEach(breed => console.log(`I love ${breed} cats!`));
filter()
: The Selector
The filter()
method returns a new array containing elements that pass a test provided as a function.
const longNamedBreeds = catBreeds.filter(breed => breed.length > 6);
console.log(longNamedBreeds);
// Output: ["Persian", "Maine Coon", "Bengal"]
Original array: ['Siamese', 'Persian', 'Maine Coon', 'Sphynx', 'Bengal'];
reduce()
: The Accumulator
The reduce()
method applies a function against an accumulator and each element in the array to reduce it to a single value.
const concatenatedBreeds = catBreeds.reduce((acc, breed) => acc + breed + ', ', '');
console.log(concatenatedBreeds);
// Output: "Siamese, Persian, Maine Coon, Sphynx, Bengal, "
Original array: ['Siamese', 'Persian', 'Maine Coon', 'Sphynx', 'Bengal'];
reduceRight()
: The Right-to-Left Accumulator
Similar to reduce()
, but works from right to left.
const reverseConcatenatedBreeds = catBreeds.reduceRight((acc, breed) => acc + breed + ', ', '');
console.log(reverseConcatenatedBreeds);
// Output: "Bengal, Sphynx, Maine Coon, Persian, Siamese, "
Original array: ['Siamese', 'Persian', 'Maine Coon', 'Sphynx', 'Bengal'];
toString()
: The String Converter
Converts the array into a string.
const breedsAsString = catBreeds.toString();
console.log(breedsAsString);
// Output: "Siamese,Persian,Maine Coon,Sphynx,Bengal"
Original array: ['Siamese', 'Persian', 'Maine Coon', 'Sphynx', 'Bengal'];
join()
: The Custom String Connector
Joins the elements into a string separated by a custom string.
const joinedBreeds = catBreeds.join(" - ");
console.log(joinedBreeds);
// Output: "Siamese - Persian - Maine Coon - Sphynx - Bengal"
Original array: ['Siamese', 'Persian', 'Maine Coon', 'Sphynx', 'Bengal'];
pop()
: The Last Element Remover
Removes the last element of the array.
const poppedBreed = catBreeds.pop();
console.log(poppedBreed);
// Output: "Bengal"
console.log(catBreeds);
// Output: ["Siamese", "Persian", "Maine Coon", "Sphynx"]
Original array: ['Siamese', 'Persian', 'Maine Coon', 'Sphynx', 'Bengal'];
push()
: The Last Element Adder
Adds an element to the end of the array.
catBreeds.push("Ragdoll");
console.log(catBreeds);
// Output: ["Siamese", "Persian", "Maine Coon", "Sphynx", "Ragdoll"]
Original array: ['Siamese', 'Persian', 'Maine Coon', 'Sphynx', 'Bengal'];
shift()
: The First Element Remover
Removes the first element of the array.
const shiftedBreed = catBreeds.shift();
console.log(shiftedBreed);
// Output: "Siamese"
console.log(catBreeds);
// Output: ["Persian", "Maine Coon", "Sphynx", "Ragdoll"]
Original array: ['Siamese', 'Persian', 'Maine Coon', 'Sphynx', 'Bengal'];
unshift()
: The First Element Adder
Adds an element to the beginning of the array.
catBreeds.unshift("Siamese");
console.log(catBreeds);
// Output: ["Siamese", "Persian", "Maine Coon", "Sphynx", "Ragdoll"]
Original array: ['Siamese', 'Persian', 'Maine Coon', 'Sphynx', 'Bengal'];
splice()
: The Element Injector/Remover
Adds or removes elements from the array.
catBreeds.splice(2, 1, "Scottish Fold");
console.log(catBreeds);
// Output: ["Siamese", "Persian", "Scottish Fold", "Sphynx", "Ragdoll"]
Original array: ['Siamese', 'Persian', 'Maine Coon', 'Sphynx', 'Bengal'];
concat()
: The Array Joiner
Joins two or more arrays.
const moreBreeds = ["Abyssinian", "Manx"];
const allBreeds = catBreeds.concat(moreBreeds);
console.log(allBreeds);
// Output: ["Siamese", "Persian", "Scottish Fold", "Sphynx", "Ragdoll", "Abyssinian", "Manx"]
Original array: ['Siamese', 'Persian', 'Maine Coon', 'Sphynx', 'Bengal'];
slice()
: The Sub-array Extractor
Returns a shallow copy of a portion of an array.
const someBreeds = catBreeds.slice(1, 4);
console.log(someBreeds);
// Output: ["Persian", "Scottish Fold", "Sphynx"]
Original array: ['Siamese', 'Persian', 'Maine Coon', 'Sphynx', 'Bengal'];
sort()
: The Sorter
Sorts the elements of an array.
const sortedBreeds = catBreeds.sort();
console.log(sortedBreeds);
// Output: ["Persian", "Ragdoll", "Scottish Fold", "Siamese", "Sphynx"]
Original array: ['Siamese', 'Persian', 'Maine Coon', 'Sphynx', 'Bengal'];
reverse()
: The Reverser
Reverses the elements of an array.
const reversedBreeds = catBreeds.reverse();
console.log(reversedBreeds);
// Output: ["Sphynx", "Siamese", "Scottish Fold", "Ragdoll", "Persian"]
Original array: ['Siamese', 'Persian', 'Maine Coon', 'Sphynx', 'Bengal'];
Leave a Reply