DEV Community

David Bell
David Bell

Posted on • Originally published at davidbell.space

10 1

Using some(), every() and Object.values to check values from within an object

Often you want to check if a value is within an object. Heres a way we can do this with some() every() and Object.values().

Object.values

Object.values is used to create an array from the values in an Object. You pass an object into Object.values and it returns an array of the values.

const stock = {
  meat: 5,
  fruit: 10,
  cheese: 6,
}
const valueArray = Object.values(stock)
// [ 5, 10, 6 ]
Enter fullscreen mode Exit fullscreen mode

some()

some() returns true if one thing passes the test depending on the condition passed in. If not it will return false.

const stock = {
  meat: 5,
  fruit: 10,
  cheese: 6,
}
const enoughStock = Object.values(stock).some(el => el >= 6)
console.log(enoughStock)
// true
Enter fullscreen mode Exit fullscreen mode
const kidsParty = {
  suzy: 29,
  bill: 4,
  tim: 2,
  sally: 5,
  ming: 3,
}
const isAdultPresent = Object.values(kidsParty).some(el => el >= 18)
console.log(isAdultPresent)
// true
Enter fullscreen mode Exit fullscreen mode

every()

every() returns true or false depending on the condition if every thing passes the test depending on the condition.

const nightclub = {
  dave: 26,
  sally: 23,
  mike: 17,
  lucy: 18,
}
const isOldEnough = Object.values(nightclub).every(el => el >= 18)
// false

if (!isOldEnough) {
  console.log("Check every ID")
}
Enter fullscreen mode Exit fullscreen mode

Let's connect

Twitter

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay