DEV Community

Luis A.
Luis A.

Posted on

One Wizard to Rule Them All: The Magic of Array.prototype.some()

Hey there, code sorcerers! 🧙‍♂️ Are you ready for a spellbinding adventure in the enchanted realm of JavaScript? Today, we're going to explore the mystical world of Array.prototype.some(), a method that can reveal the hidden secrets of your arrays with a mere flick of your wand. 🪄

Join me on this magical journey, and let's unravel the arcane mysteries of this spellbinding method that'll help you find that one special item in your array! 🧙‍♂️✨

A sample array

Suppose we've got an array of wizards. Each item is an object with a name, spells, and tool property. It's like a magical version of Pokémon! 🧙‍♂️✨


let wizards = [
    {
        name: 'Radagast',
        spells: ['Talk to animals', 'Grow plants'],
        tool: 'staff'
    },
    {
        name: 'Merlin',
        spells: ['Dancing teacups', 'Turn into fish'],
        tool: 'wand'
    },
    {
        name: 'Gandalf',
        spells: ['You shall not pass', 'Disappear'],
        tool: 'staff'
}
];

Enter fullscreen mode Exit fullscreen mode

The Array.prototype.some() method

The Array.prototype.some() method is like that one friend who's satisfied as long as there's one slice of pizza left at the party 🍕. It accepts a callback function as an argument and will return true if at least one item in the array meets the criteria.



wizards.some(function (wizard, index, arr) {
    // do magical stuff...
});

Enter fullscreen mode Exit fullscreen mode

Inside the callback function, you can test each item in the array and return a boolean value (true or false). If at least one item returns true, the Array.prototype.some() method returns true. Otherwise, it returns false.

Let's look at some examples to show the difference between Array.prototype.every() and Array.prototype.some().

Example 1: check if at least one wizard uses a staff

Imagine we wanna check if at least one wizard in the array uses a staff as their tool. It's like a magical game of "Where's Waldo?" 🧙‍♂️



// returns true
// (Both Radagast and Gandalf use staffs)
let hasStaff = wizards.some(function (wizard) {
    return wizard.tool === 'staff';
});

Enter fullscreen mode Exit fullscreen mode

Example 2: check if at least one wizard has at least one spell

We could also use the Array.prototype.some() method to check if any wizard in the array has at least one spell. After all, what's a wizard without their spells? 🧙‍♂️💫


// returns true
let hasSpells = wizards.some(function (wizard) {
    return wizard.spells.length > 0;
});

Enter fullscreen mode Exit fullscreen mode

A more complex example: checking for a spell

Let's imagine we want to make sure at least one wizard in our array can cast the spell "You shall not pass". Maybe we're planning a surprise party for Balrog, and we need someone to keep him out? 🧙‍♂️🎉

Inside the callback function, we could use the Array.prototype.includes() method on the spells array to look for our spell.


// returns true
let doNotPass = wizards.some(function (wizard) {
    return wizard.spells.includes('You shall not pass');
});

Enter fullscreen mode Exit fullscreen mode

And there you have it, folks! The Array.prototype.some() method is like our magical MVP, making sure at least one item in our array is up to snuff. So the next time you're on a wizard hunt, remember this spell! 🧙‍♂️🔮

Top comments (3)

Collapse
 
developerdoran profile image
Jake Doran

Educational, entertaining and well written - great post!

Collapse
 
almonteluis profile image
Luis A.

I appreciate it kind sir

Collapse
 
developerdoran profile image
Jake Doran

You're welcome!