Master JavaScript arrays with this comprehensive cheatsheet! This comprehensive JavaScript array cheatsheet is the perfect resource for anyone who wants to learn everything there is to know about arrays in JavaScript. Covering a wide range of topics, from basic array operations to advanced array methods, this cheatsheet has everything you need to start using arrays effectively in your code.
📘 15+ Best JavaScript Books for Beginners and Experienced Coders
Whether you're a beginner or an experienced programmer, this cheatsheet is a must-have for anyone who wants to learn JavaScript arrays.
Returns a new array with the results of calling a provided function on every element in this array.
const list = [😫, 😫, 😫, 😫];
list.map((⚪️) => 😀); // [😀, 😀, 😀, 😀]
// Code
const list = [1, 2, 3, 4];
list.map((el) => el * 2); // [2, 4, 6, 8]
Returns a new array with all elements that pass the test implemented by the provided function.
const list = [😀, 😫, 😀, 😫];
list.filter((⚪️) => ⚪️ === 😀); // [😀, 😀]
// Code
const list = [1, 2, 3, 4];
list.filter((el) => el % 2 === 0); // [2, 4]
Reduce the array to a single value. The value returned by the function is stored in an accumulator (result/total).
const list = [😀, 😫, 😀, 😫, 🤪];
list.reduce((⬜️, ⚪️) => ⬜️ + ⚪️); // 😀 + 😫 + 😀 + 😫 + 🤪
// OR
const list = [1, 2, 3, 4, 5];
list.reduce((total, item) => total + item, 0); // 15
Executes a reducer function (that you provide) on each element of the array resulting in a single output value(from right to left).
const list = [😀, 😫, 😀, 😫, 🤪];
list.reduceRight((⬜️, ⚪️) => ⬜️ + ⚪️); // 🤪 + 😫 + 😀 + 😫 + 😀
// Code
const list = [1, 2, 3, 4, 5];
list.reduceRight((total, item) => total + item, 0); // 15
Fill the elements in an array with a static value.
const list = [😀, 😫, 😀, 😫, 🤪];
list.fill(😀); // [😀, 😀, 😀, 😀, 😀]
// Code
const list = [1, 2, 3, 4, 5];
list.fill(0); // [0, 0, 0, 0, 0]
Returns the value of the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.
const list = [😀, 😫, 😀, 😫, 🤪];
list.find((⚪️) => ⚪️ === 😀); // 😀
list.find((⚪️) => ⚪️ === 😝); // undefined
// Code
const list = [1, 2, 3, 4, 5];
list.find((el) => el === 3); // 3
list.find((el) => el === 6); // undefined
Returns the first index at which a given element can be found in the array, or -1 if it is not present.
const list = [😀, 😫, 😀, 😫, 🤪];
list.indexOf(😀); // 0
list.indexOf(😡); // -1
// Code
const list = [1, 2, 3, 4, 5];
list.indexOf(3); // 2
list.indexOf(6); // -1
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.
const list = [😀, 😫, 😀, 😫, 🤪];
list.lastIndexOf(😀); // 3
list.lastIndexOf(😀, 1); // 0
// Code
const list = [1, 2, 3, 4, 5];
list.lastIndexOf(3); // 2
list.lastIndexOf(3, 1); // -1
Returns the index of the first element in the array that satisfies the provided testing function. Otherwise -1 is returned.
const list = [😀, 😫, 😀, 😫, 🤪];
list.findIndex((⚪️) => ⚪️ === 😀); // 0
// You might be thinking how it's different from `indexOf` 🤔
const array = [5, 12, 8, 130, 44];
array.findIndex((element) => element > 13); // 3
// OR
const array = [{
id: 😀
}, {
id: 😫
}, {
id: 🤪
}];
array.findIndex((element) => element.id === 🤪); // 2
Returns true if the given element is present in the array.
const list = [😀, 😫, 😀, 😫, 🤪];
list.includes(😀); // true
// Code
const list = [1, 2, 3, 4, 5];
list.includes(3); // true
list.includes(6); // false
Removes the last element from an array and returns that element.
const list = [😀, 😫, 😀, 😫, 🤪];
list.pop(); // 🤪
list; // [😀, 😫, 😀, 😫]
// Code
const list = [1, 2, 3, 4, 5];
list.pop(); // 5
list; // [1, 2, 3, 4]
Appends new elements to the end of an array, and returns the new length.
const list = [😀, 😫, 😀, 😫, 🤪];
list.push(😡); // 5
list; // [😀, 😫, 😀, 😫, 🤪, 😡]
// Code
const list = [1, 2, 3, 4, 5];
list.push(6); // 6
list; // [1, 2, 3, 4, 5, 6]
Removes the first element from an array and returns that element.
const list = [😀, 😫, 😀, 😫, 🤪];
list.shift(); // 😀
list; // [😫, 😀, 😫, 🤪]
// Code
const list = [1, 2, 3, 4, 5];
list.shift(); // 1
list; // [2, 3, 4, 5]
Adds new elements to the beginning of an array, and returns the new length.
const list = [😀, 😫, 😀, 😫, 🤪];
list.unshift(😡); // 6
list; // [😡, 😀, 😫, 😀, 😫, 🤪]
// Code
const list = [1, 2, 3, 4, 5];
list.unshift(0); // 6
list; // [0, 1, 2, 3, 4, 5]
Changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
const list = [😀, 😫, 😀, 😫, 🤪];
list.splice(1, 2); // [😀, 😫]
list; // [😀, 😫, 🤪]
// Code
const list = [1, 2, 3, 4, 5];
list.splice(1, 2); // [2, 3]
list; // [1, 4, 5]
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.
const list = [😀, 😫, 😀, 😫, 🤪];
list.slice(1, 3); // [😫, 😀]
list; // [😀, 😫, 😀, 😫, 🤪]
// Code
const list = [1, 2, 3, 4, 5];
list.slice(1, 3); // [2, 3]
list; // [1, 2, 3, 4, 5]
Joins all elements of an array into a string.
const list = [😀, 😫, 😀, 😫, 🤪];
list.join('〰️'); // "😀〰️😫〰️😀〰️😫〰️🤪"
// Code
const list = [1, 2, 3, 4, 5];
list.join(', '); // "1, 2, 3, 4, 5"
Reverses the order of the elements in an array.
const list = [😀, 😫, 😀, 😫, 🤪];
list.reverse(); // [🤪, 😫, 😀, 😫, 😀]
list; // [🤪, 😫, 😀, 😫, 😀]
// Code
const list = [1, 2, 3, 4, 5];
list.reverse(); // [5, 4, 3, 2, 1]
list; // [5, 4, 3, 2, 1]
Sorts the elements of an array in place and returns the array. The default sort order is according to string Unicode code points.
const list = [😀, 😫, 😀, 😫, 🤪];
list.sort(); // [😀, 😀, 😫, 😫, 🤪]
// This make more sense 🤔
const array = ['D', 'B', 'A', 'C'];
array.sort(); // 😀 ['A', 'B', 'C', 'D']
// OR
const array = [4, 1, 3, 2, 10];
array.sort(); // 😧 [1, 10, 2, 3, 4]
array.sort((a, b) => a - b); // 😀 [1, 2, 3, 4, 10]
Returns true if at least one element in the array passes the test implemented by the provided function.
const list = [😀, 😫, 😀, 😫, 🤪];
list.some((⚪️) => ⚪️ === 😀); // true
list.some((⚪️) => ⚪️ === 😡); // false
// Code
const list = [1, 2, 3, 4, 5];
list.some((el) => el === 3); // true
list.some((el) => el === 6); // false
Returns true if all elements in the array pass the test implemented by the provided function.
const list = [😀, 😫, 😀, 😫, 🤪];
list.every((⚪️) => ⚪️ === 😀); // false
const list = [😀, 😀, 😀, 😀, 😀];
list.every((⚪️) => ⚪️ === 😀); // true
// Code
const list = [1, 2, 3, 4, 5];
list.every((el) => el === 3); // false
const list = [2, 4, 6, 8, 10];
list.every((el) => el%2 === 0); // true
Creates a new array from an array-like or iterable object.
const list = 😀😫😀😫🤪;
Array.from(list); // [😀, 😫, 😀, 😫, 🤪]
const set = new Set(['😀', '😫', '😀', '😫', '🤪']);
Array.from(set); // [😀, 😫, 🤪]
const range = (n) => Array.from({ length: n }, (_, i) => i + 1);
console.log(range(10)); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Creates a new array with a variable number of arguments, regardless of number or type of the arguments.
const list = Array.of(😀, 😫, 😀, 😫, 🤪);
list; // [😀, 😫, 😀, 😫, 🤪]
// Code
const list = Array.of(1, 2, 3, 4, 5);
list; // [1, 2, 3, 4, 5]
Returns true if the given value is an array.
Array.isArray([😀, 😫, 😀, 😫, 🤪]); // true
Array.isArray(🤪); // false
// Code
Array.isArray([1, 2, 3, 4, 5]); // true
Array.isArray(5); // false
Returns a value at the specified index.
const list = [😀, 😫, 😀, 😫, 🤪];
list.at(1); // 😫
// Return from last 🤔
list.at(-1); // 🤪
list.at(-2); // 😫
// Code
const list = [1, 2, 3, 4, 5];
list.at(1); // 2
list.at(-1); // 5
list.at(-2); // 4
Copies array elements within the array. Returns the modified array.
const list = [😀, 😫, 😀, 😫, 🤪];
list.copyWithin(1, 3); // [😀, 😀, 🤪, 😫, 🤪]
const list = [😀, 😫, 😀, 😫, 🤪];
list.copyWithin(0, 3, 4); // [😫, 😫, 😀, 😫, 🤪]
// Code
const list = [1, 2, 3, 4, 5];
list.copyWithin(0, 3, 4); // [4, 2, 3, 4, 5]
NOTE: 🤔
Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.
const list = [😀, 😫, [😀, 😫, 🤪]];
list.flat(Infinity); // [😀, 😫, 😀, 😫, 🤪]
// Code
const list = [1, 2, [3, 4, [5, 6]]];
list.flat(Infinity); // [1, 2, 3, 4, 5, 6]
Returns a new array formed by applying a given callback function to each element of the array,
const list = [😀, 😫, [😀, 😫, 🤪]];
list.flatMap((⚪️) => [⚪️, ⚪️ + ⚪️ ]); // [😀, 😀😀, 😫, 😫😫, 😀, 😀😀, 😫, 😫😫, 🤪, 🤪🤪]
// Code
const list = [1, 2, 3];
list.flatMap((el) => [el, el * el]); // [1, 1, 2, 4, 3,
var meals = ['breakfast', 'lunch', 'dinner'] ;
Keeping references intact.
var meals = ['breakfast', 'lunch', 'dinner'];
meals.splice(0, meals.length);
or
var meals = ['breakfast', 'lunch', 'dinner'];
meals.length = 0
var meals = ['breakfast', 'lunch', 'dinner'];
var copy = meals.slice();
// ['breakfast', 'lunch', 'dinner']
var meals = ['breakfast', 'lunch', 'dinner'];
meals[meals.length - 1];
// 'dinner'
Or
var meals = ['breakfast', 'lunch', 'dinner'];
meals.slice(-1)[0];
// 'dinner'
var meals = ['breakfast', 'lunch', 'dinner'];
meals.shift();
// 'breakfast'
meals;
// ['lunch', 'dinner']
var meals = ['breakfast', 'lunch', 'dinner'];
meals.pop();
// 'dinner'
meals;
// ['breakfast', 'lunch'];
var meals = ['lunch', 'dinner'];
meals.unshift('breakfast');
// 3 - the array length
meals;
// ['breakfast', 'lunch', 'dinner']
var meals = ['breakfast', 'lunch', 'dinner'];
meals.push('supper');
// 2
meals;
// ['breakfast', 'lunch', 'dinner', 'supper'];
var meals = ['breakfast', 'lunch', 'dinner'];
meals[1] = 'brunch';
// ['breakfast', 'brunch', 'dinner'];
Or
var meals = ['breakfast', 'lunch', 'dinner'];
meals.splice(1, 1, 'brunch');
var meals = ['breakfast', 'lunch', 'dinner'];
meals.splice(1, 0, 'brunch', 'more brunch');
// ['breakfast', 'brunch', 'more brunch', 'lunch', 'dinner']
var meals = ['breakfast', 'lunch', 'dinner'];
meals.splice(1, 1);
// ['lunch']
meals;
// ['breakfast', 'dinner']
var meals = ['breakfast', 'lunch', 'dinner'];
meals.splice(1, 2);
// ['lunch', 'dinner']
meals;
// ['breakfast']
var meals = ['breakfast', 'lunch', 'dinner'];
meals.reverse();
// ['dinner', 'lunch', 'breakfast'];
var meals = ['breakfast', 'lunch', 'dinner']; meals.join(' AND '); // 'breakfast AND lunch AND dinner'
var meals = ['dinner', 'supper', 'breakfast', 'lunch'];
meals.sort();
// ['breakfast', 'dinner', 'lunch', 'supper']
var numbers = [1438,2605,794,3947,6241,11745,2585];
numbers.sort(function(a, b) {
return a - b;
});
// [794,1438,2585,2605,3947,6241,11745]
var dayTimeMeals = ['breakfast', 'lunch'];
var nightTimeMeals = ['merienda', 'dinner'];
var allTheMeals = dayTimeMeals.concat(nightTimeMeals);
// ['breakfast', 'lunch', 'merienda', 'dinner']
var meals = ['breakfast', 'lunch', 'dinner', 'supper'];
nightTimeMeals = meals.slice(2,4);
// ['dinner', 'supper']
var meals = ['breakfast', 'lunch', 'dinner'];
var type = ['king', 'prince', 'pauper'];
meals.map(function(item, i) {
return item + ' like a ' + type[i];
});
// ["breakfast like a king", "lunch like a prince", "dinner like a pauper"]
var meals = ['breakfast', 'lunch', 'dinner', 'supper'];
meals.every(function(item){ return item.length > 0 });
// true
meals.every(function(item){ return item.length > 6 });
// false
var meals = ['breakfast', 'lunch', 'dinner', 'supper'];
meals.some(function(item){ return item === 'lunch';});
// true
meals.some(function(item){ return item === 'burgers!!';});
//false
var meals = ['breakfast', 'lunch', 'dinner', 'supper'];
meals.forEach(function(currentValue, index, arr){
console.log(index, currentValue, arr);
});
var meals = ['breakfast', 'lunch', 'dinner', 'supper'];
meals.filter(function(item) {
return item !== 'breakfast';
});
// ['lunch', 'dinner', 'supper'];
var meals = ['breakfast', 'lunch', 'dinner'];
Array.isArray(meals)
// true
var meals = ['breakfast', 'lunch', 'dinner'];
function isArray(arr) {
return !!(Object.prototype.toString.call(arr) === '[object Array]');
}
isArray(meals);
// true
var meals = ['breakfast', 'elevenses', 'brunch'];
meals.shift();
meals.push('lunch');
// ['elevenses', 'brunch', 'lunch']
meals.shift()
meals.push('afternoon tea');
// ['brunch', 'lunch', 'afternoon tea']
// ... and so on ...
var meals = ['breakfast', 'elevenses', 'brunch'];
meals.indexOf('brunch');
// 2
var meals = ['breakfast', 'elevenses', 'brunch'];
function inArray(arr, item){
var found = -1,
i = arr.length;
while(--i >= 0) {
if(arr[i] === item){
found = i;
break;
}
}
return found;
}
inArray(meals, 'brunch');
// 2 - the index of the item in the array
inArray(meals, 'dinner');
// -1
function randomiseArray(arr) {
var buffer = [], start;
for(var i = arr.length; i >= arr.length && i > 0;i--) {
start = Math.floor(Math.random() * arr.length);
buffer.push(arr.splice(start, 1)[0])
};
return buffer;
}
randomiseArray([0,1,2,3,4]);
// [2,1,0,3,4]
randomiseArray([0,1,2,3,4]);
// [3,2,1,4,0]
randomiseArray([0,1,2,3,4]);
// [1,2,4,0,3]
Chaining methods
var meals = [
{type: 'breakfast', name: 'Full English', calories: 1500},
{type: 'breakfast', name: 'Colacao', calories: 260},
{type: 'breakfast', name: 'Croissant and jam', calories: 520},
{type: 'breakfast', name: 'Granola with Greek yoghurt and blueberries', calories: 680},
{type: 'brinner', name: 'Shepherds Pie with strawberry yoghurt', calories: 915},
{type: 'brinner', name: 'Milky Porridge with beef and green beans', calories: 875},
{type: 'dinner', name: 'Phad Thai', calories: 750},
{type: 'dinner', name: 'Chicken Katsu curry and rice', calories: 830},
];
function getMealsByMaxCalories(meals, maxCalories, dailyAllowance) {
return meals
.filter(function(meal) {
return meal.calories <= maxCalories;
})
.map(function(meal) {
return {
name: meal.name,
calories: meal.calories,
percentageOfDailyAllowance: meal.calories / dailyAllowance * 100
}
});
}
getMealsByMaxCalories(meals, 850, 2000);
/*
[
{
"name": "Colacao",
"calories": 260,
"percentageOfDailyAllowance": 13
},
{
"name": "Croissant and jam",
"calories": 520,
"percentageOfDailyAllowance": 26
},
{
"name": "Granola with Greek yoghurt and blueberries",
"calories": 680,
"percentageOfDailyAllowance": 34
},
{
"name": "Phad Thai",
"calories": 750,
"percentageOfDailyAllowance": 37.5
}
]
*/
#javascript #js #array