DEV Community

Cover image for JavaScript some() method
Chris Bongers
Chris Bongers

Posted on β€’ Originally published at daily-dev-tips.com

12 2

JavaScript some() method

Did you ever need to know if one of the elements in an array passed a test?

This is where the some() method comes in handy.

Let's keep using our product array, but let's add a discounted product.

We then want to test if some of our products are discounted.

Using the Javascript some() method

Let's start by creating an array of items.

const items = [
  { name: 'T-shirt plain', price: 9, discount: true },
  { name: 'T-shirt print', price: 20 },
  { name: 'Jeans', price: 30 },
  { name: 'Cap', price: 5 }
];
Enter fullscreen mode Exit fullscreen mode

Now let's use the some() method to test if we have a discounted product in our array.

const discounted = items.some(item => {
  return item.discount;
});

// Returns true
Enter fullscreen mode Exit fullscreen mode

If we now remove the discount on our item, it will return false.

Another use case might be that you need to check if all people are under a certain age.

const users = [
  { name: 'Bob', age: 60 },
  { name: 'Sarah', age: 20 },
  { name: 'Billy', age: 18 },
];

const ageRestriction = users.some(user => {
  return user.age <= 18;
});

// Returns true
Enter fullscreen mode Exit fullscreen mode

This return true, because billy is under the age of 18!

The syntax for some is as follows:

const new = original.some(function(value));
Enter fullscreen mode Exit fullscreen mode

Inside our function, we can check on certain properties the value has.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Image of Bright Data

High-Quality Data for AI – Access diverse datasets ready for your ML models.

Browse our extensive library of pre-collected datasets tailored for various AI and ML projects.

Explore Datasets

Top comments (4)

Collapse
 
chukwu3meka profile image
Chukwuemeka Maduekwe β€’

I love this post, but I don't think the second answer will be true since its < and not <= you used there

Collapse
 
defite profile image
Nikita Makhov β€’

Yep. It will be false.

Collapse
 
dailydevtips1 profile image
Chris Bongers β€’

Ah woops, yes should be <= my bad.

Collapse
 
j23saw profile image
j23saw β€’

True, OP please edit the post

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay