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
- Python
- Python Flask
- Report
- Security
- Server
- SpringBoot
- Subdomain
- TypeScript
- Uncategorized
- VSCode
- Webhosting
- WordPress
Search
Recent Post
How to Create CRON Job in Linux
- 6 December, 2022
- 7 min read
How to manage networking configuration in Linux
- 29 November, 2022
- 10 min read
How to Add Angular Material UI Table
- 17 November, 2022
- 17 min read