JavaScript Array Methods Cheatsheet:13 Handy Tricks for Developers

Arrays are everywhere in JavaScript. Whether you’re building a simple to-do list or a complex web app, you’ll definitely work with them. So why not get smarter about it? In this cheatsheet, we’ll walk you through 13 incredibly useful and easy-to-understand JavaScript array tricks that can help improve your code instantly!

JavaScript Array Methods Cheatsheet:13 Handy Tricks for Developers

1. Remove Duplicates from an Array

Need to get unique values from an array? Use the Set object to do it in one line!

const fruits = ["banana", "apple", "orange", "watermelon", "apple"];
const uniqueFruits = [...new Set(fruits)];
console.log(uniqueFruits); // ["banana", "apple", "orange", "watermelon"]

2. Replace a Specific Value

Use splice() to replace any item at a specific index.

const fruits = ["banana", "apple", "orange"];
fruits.splice(1, 1, "grape");
console.log(fruits); // ["banana", "grape", "orange"]

3. Map Without .map()

You can also use Array.from() with a mapping function to transform arrays.

const friends = [
  { name: "John" }, { name: "Jane" }
];
const names = Array.from(friends, ({ name }) => name);
console.log(names); // ["John", "Jane"]

4. Empty an Array

Want to clear out all items from an array? Set its length to 0.

let fruits = ["banana", "apple"];
fruits.length = 0;
console.log(fruits); // []

5. Convert Array to Object

Use the spread operator to turn an array into an object with numeric keys.

const fruits = ["banana", "apple"];
const fruitObj = { ...fruits };
console.log(fruitObj); // {0: "banana", 1: "apple"}

6. Fill Array with Values

fill() helps you create arrays filled with the same value.

const filledArray = new Array(5).fill("JS");
console.log(filledArray); // ["JS", "JS", "JS", "JS", "JS"]

7. Merge Multiple Arrays

Combine multiple arrays using the spread operator.

const a = [1, 2];
const b = [3, 4];
const c = [...a, ...b];
console.log(c); // [1, 2, 3, 4]

8. Find the Intersection of Two Arrays

Use filter() and includes() to get common elements.

const a = [1, 2, 3];
const b = [2, 3, 4];
const common = [...new Set(a)].filter(val => b.includes(val));
console.log(common); // [2, 3]

9. Remove Falsy Values

Quickly filter out values like undefined, null, 0, and "".

const mixed = [0, "", 2, null, "hello"];
const clean = mixed.filter(Boolean);
console.log(clean); // [2, "hello"]

10. Get a Random Value

Select a random item from an array using Math.random().

const colors = ["red", "blue", "green"];
const random = colors[Math.floor(Math.random() * colors.length)];
console.log(random);

11. Reverse an Array

Flip the order of items using reverse().

const numbers = [1, 2, 3];
numbers.reverse();
console.log(numbers); // [3, 2, 1]

12. Find Last Index of a Value

Use lastIndexOf() to find the last position of a value.

const nums = [1, 2, 3, 2];
const last = nums.lastIndexOf(2);
console.log(last); // 3

13. Sum All Values

Use reduce() to sum all elements of a numeric array.

const nums = [1, 2, 3];
const sum = nums.reduce((a, b) => a + b, 0);
console.log(sum); // 6

Final Thoughts

JavaScript arrays are powerful and flexible. These 13 tricks make your code cleaner and more efficient. Bookmark this cheatsheet and revisit it whenever you're working with arrays. Happy coding!

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad