This article is a JavaScript Arrays Cheat Sheet. Basically, we are going to look at a list of JavaScript 1 liner code snippets for Arrays. It is immensely useful when you’re looking to quickly perform some menial or important tasks where you don’t want to use loops. So, let’s begin.
Prerequisite
- Familiar with JavaScript Syntax
- Working Knowledge About Arrays
Basic Methods
A. JavaScript Array concat
[1,2,3].concat([4,5])
OUTPUT = [1,2,3,4,5]
Concatenate the array passed to concat to the end of the array Add the elements 4 and 5 to the end of the array
B. JavaScript Array slice
[1,2,3,4].slice(1, 3)
OUTPUT = [2,3]
Copy part of an array starting at the index of the first parameter and stopping at the index of the second parameter, but not including the last index Copy the values in index 1 and 2 into a new array
C. JavaScript Array reverse
[1,2,3].reverse()
OUTPUT = [3,2,1]
Reverse the order of the elements in an array
D. JavaScript Array join
[1,2,3].join(",")
OUTPUT = ‘1,2,3’
Combine each element of the array into a string with the value passed to join placed between each value Add a comma and a space between each element in the array
Array Manipulation
A. JavaScript Array push
[1,2,3].push(4, 5)
OUTPUT = [1,2,3,4,5]
Add one or more elements to the end of the array Add 4 and 5 to the end of the array. Returns length of the array.
B. JavaScript Array pop
[1,2,3].pop()
OUTPUT = [1,2]
Remove the last element from the array Remove the value 3 from the array
C. JavaScript Array shift
[1,2,3].shift()
OUTPUT = [2,3]
Remove the first element from the array Remove the value 1 from the array
D. JavaScript Array unshift
[1,2,3].unshift(4, 5)
OUTPUT = [4,5,1,2,3]
Add one or more elements to the start of the array Add 4 and 5 to the start of the array
E. JavaScript Array splice
[1,2,3].splice(1, 1, 4, 5)
OUTPUT = [1,4,5,3]
Starting at the index specified by the first parameter, remove the number of elements specified by the second parameter and then add elements to the array for all remaining parameters at the index specified by the first parameter Starting at index 1, remove 1 element and then add the elements 4 and 5 at index 1.
F. JavaScript Array forEach
[1,2,3].forEach(n => console.log(n))
OUTPUT = 1 2 3
Execute the function passed to forEach for each element in the array similar to a for loop Log out each element in the array.
G. JavaScript Array map
[1,2,3].map(n => n * 2)
OUTPUT = [2,4,6]
Return a new array that contains the return result of running each item through the function passed to map Create a new array with all values doubled
H. JavaScript Array filter
[1,2,3].filter(n => n > 2)
OUTPUT = [3]
Return a new array that only contains values that return true to the function passed to filter Create a new array with only values greater than 2
I. JavaScript Array find
[1,2,3].find(n => n > 1)
OUTPUT = [2]
Select the first element that returns true to the function passed to find Get the first element greater than 1
J. JavaScript Array findIndex
[1,2,3].findIndex(n => n > 1)
OUTPUT = 1
Get the index of the first element that returns true to the function passed to find Get the index of the first element greater than 1
K. JavaScript Array every
[1,2,3].every(n => n > 1)
OUTPUT = false
Return true if every element in the array returns true from the function passed to every Check if every element is greater than 1
L. JavaScript Array some
[1,2,3].some(n => n > 1)
OUTPUT = true
Return true if at least one element in the array returns true from the function passed to some Check if any element is greater than 1
M. JavaScript Array reduce
[1,2,3].reduce((sum, n) => sum + n, 0)
OUTPUT = 6
Reduce the array to one single value by starting the sum at the second value passed to reduce and updating the sum with the return value of each iteration Sum all the numbers in the array starting with 0
Basic Value Checks
A. JavaScript Array indexOf
[1,2,1].indexOf(1)
OUTPUT = 0
Return the first index of the value passed to indexOf or return -1 if the value cannot be found Get the first index of 1 in the array
B. JavaScript Array lastIndexOf
[1,2,1].lastIndexOf(1)
OUTPUT = 2
Return the last index of the value passed to indexOf or return -1 if the value cannot be found Get the last index of 1 in the array
C. JavaScript Array includes
[1,2,3].includes(4)
OUTPUT = false
Return true if the value passed to includes is in the array Check if the value 4 is in the array
D. JavaScript Array length
[1,2,3].length
OUTPUT = s3
Return the length of the array
And that’s a wrap!
Thank you for going through my article, JavaScript Arrays Cheat Sheet. I hope that you found it useful and were able to comprehend it. Feel free to share your thoughts and opinions, in the comments section down below. Looking forward to an optimistic response!
Have a great one!
Recent Comments
Categories
- Angular
- AWS
- Backend Development
- Big Data
- Cloud
- Database
- Deployment
- DevOps
- Docker
- Frontend Development
- GitHub
- Google Cloud Platform
- Installations
- Java
- JavaScript
- Linux
- MySQL
- Networking
- NodeJS
- Operating System
- Python
- Python Flask
- Report
- Security
- Server
- SpringBoot
- Subdomain
- TypeScript
- Uncategorized
- VSCode
- Webhosting
- WordPress
Search
Recent Post
Understanding Mutex, Semaphores, and the Producer-Consumer Problem
- 13 October, 2024
- 10 min read
Process scheduling algorithm – FIFO SJF RR
- 14 September, 2024
- 8 min read
How to Implement Multithreading in C Language
- 8 September, 2024
- 9 min read