map() method helps you transform your array without changing the original one.
const numbers = [1, 2, 3]; const doubled = numbers.map(x => x * 2); // Output: doubled = [2, 4, 6]
No, this isn't about your Instagram filter. The
filter() method helps you keep only what you want in an array.
const arr = [1, 2, 3, 4, 5]; const even = arr.filter(x => x % 2 === 0); // Output: even = [2, 4]
Want to find something quickly? Use
find() and stop playing hide-and-seek with your elements!
const animals = ['cat', 'dog', 'fish']; const findFish = animals.find(x => x === 'fish'); // Output: findFish = 'fish'
Who needs a knife when you've got
slice()? Cut your array into pieces, just like your favorite birthday cake! 🍰
const fruits = ['apple', 'banana', 'cherry']; const myFruits = fruits.slice(0, 2); // Output: myFruits = ['apple', 'banana']
Just like Marie Kondo,
reduce() helps you tidy up an array into a single value that sparks joy! 🌟
const values = [1, 2, 3, 4]; const sum = values.reduce((acc, val) => acc + val, 0); // Output: sum = 10
Looking for that special something or someone?
indexOf() helps you find the position of your 'crush element' in an array.
const crushes = ['apple', 'banana', 'cherry']; const position = crushes.indexOf('banana'); // Output: position = 1
Who said time travel isn't possible? With
reverse(), you can turn back time on your arrays!
const time = [1, 2, 3]; const reversedTime = time.reverse(); // Output: reversedTime = [3, 2, 1]
Last but not least, let's talk about
sort(). Put some order into your chaotic array life!
const chaos = [3, 1, 2]; const order = chaos.sort(); // Output: order = [1, 2, 3]
There you have it! You're now an array whisperer. 🤠 You've learned how to manipulate, transform, and basically have a party with arrays. If you have any favorite array methods or tricks up your sleeve, drop a comment below and share the wealth!
Go forth, and array like you've never arrayed before! 🎉
Feel free to comment, share, and let me know what you think! See you next time! ✌️