Eugene Ganshin
Eugene Ganshin

Reputation: 135

Check if values of objects are the same

I am trying to check if object values are the same and if so return true. For example, if all Fridays are 'ok' return true. If not return false.

{
"paul":{"Friday":"ok","Saturday":"OK","Sunday":"--"},
"peter":{"Friday":"ok","Saturday":"--","Sunday":"ok"},
"mary":{"Friday":"ok","Saturday":"OK","Sunday":"--"}
}

I tried solving it with a counter and it works but it seems robust. Are there any better solutions?

Upvotes: 1

Views: 46

Answers (1)

Pranav C Balan
Pranav C Balan

Reputation: 115222

You can use the Array#every method to check all elements in an array that satisfies certain conditions.

const obj = {
  "paul": {
    "Friday": "ok",
    "Saturday": "OK",
    "Sunday": "--"
  },
  "peter": {
    "Friday": "ok",
    "Saturday": "--",
    "Sunday": "ok"
  },
  "mary": {
    "Friday": "ok",
    "Saturday": "OK",
    "Sunday": "--"
  }
};

console.log(
  Object.values(obj).every(o => o.Friday === 'ok')
)

Upvotes: 3

Related Questions