Awesome JavaScript One-Liners For Array
S
StaticFast Team
1 min read
In JavasScript, an array is a very common data structure. We use it almost daily. In this post, we share some awesome JavaScript One-Liners code for array objects to make your frontend development life easier.
Convert an array to a string
array.join(",")
Convert an object to an array
Object.values(object)
Create a new array with a range
Array.from({ length: n }, (_, i) => i)
Check if a variable is an array
Array.isArray(variable)
Check if an array is empty
array.length === 0
Find the max value in an array
Math.max(...array)
Find the minimum value in an array
Math.min(...array)
Remove duplicates from an array
[...new Set(array)]
Count the occurrences of an element in an array
array.filter(x => x === element).length
Get the sum of all the numbers in an array
array.reduce((a, b) => a + b, 0));