filter(), and more, using emojis to illustrate each one.
Imagine you have a toy box 📦. When you get a new toy (element) 🧸, you just push it into the box from the top 📤. The box becomes bigger, and you know how many toys you have by counting them! 🎉
let fruits = ['🍏', '🍌']; fruits.push('🍊'); console.log(fruits);
Picture your shoes lined up 👟. When you get a new shoe, you add it to the front 🚶. All the shoes move, and you can see the new shoe first 👟👞.
let colors = ['🟢', '🔵']; colors.unshift('🔴'); // Adds '🔴' to the beginning console.log(colors);
Think of a stack of cups 🥤. When you take one cup from the top, the stack gets smaller 🥤. You can hold the cup you took and see how many cups are left 🥤🥤.
let stack = ['1️⃣', '2️⃣', '3️⃣']; let poppedElement = stack.pop(); // Removes and returns '3️⃣' console.log(stack)
Imagine you have a bag of marbles 🧿. You want to pick out only the shiny ones ✨. You look at each marble and keep only the shiny ones in a new bag 🛍️.
let numbers = [1, 2, 3, 4, 5]; let evenNumbers = numbers.filter(num => num % 2 === 0); // Filters even nums console.log(evenNumbers)
Pretend you're colouring a picture 🖍️. You have a blank copy, and you're copying the colours from another picture 🎨. You end up with a new colourful picture 🌈.
let squares = [1, 2, 3, 4]; let squareRoots = squares.map(num => Math.sqrt(num)); // Calcs square roots console.log(squareRoots)
Think of letters on magnets 🧲. You want to make words, so you put the magnets together 🤝. Now you have a sentence made of words 📝!
let words = ['👋', '🌍']; let sentence = words.join(' '); // Joins with space: '👋 🌍' console.log(sentence);
Imagine you have two piles of building blocks 🧱. You want to build something big, so you put the blocks from both piles together 🏗️. Now you have a bigger tower 🗼!
let arr1 = ['1️⃣', '2️⃣']; let arr2 = ['3️⃣', '4️⃣']; let combinedArray = arr1.concat(arr2); // Combines arrays console.log(combinedArray);
Think of nesting dolls 🎎. Inside a big doll, there's a smaller doll 🎈. To see all the dolls together, you take them out of each other 👐.
let nestedArray = ['1️⃣', ['2️⃣', ['3️⃣']]]; let flattenedArray = nestedArray.flat(2); // Flattens nested arrays console.log(flattenedArray);
Picture a pizza 🍕. You want a slice to eat 🍴. You cut a piece from the pizza 🍕🔪, and now you have your own slice with your favourite toppings 🍕 🍅 🧀.
let fruits = ['🍏', '🍌', '🍊', '🍇']; let slicedFruits = fruits.slice(1, 3); // Slices from index 1 to 2 console.log(slicedFruits);
May your code always be as expressive as your favourite emojis! 🎉