0

My original task was to create a function that returned a boolean value if my data object contained a truthy value.

I do not care what value or key is truthy, only that the data contains a truthy value.

var fruits = { apples: false, oranges: true, bananas: true }

When iterating over this object the return should be true because there are true values within.

The following function did work:

return Object.values(fruits).some(function(k) {
    return k;
});

However I cannot use Object.value nor the array.some due to IE compatibility

The suggested Polyfill is to use .map instead to get each value, however but the next step is removing .some() - I have tried using .filter() but this takes me back to the original problem that it returns the key that is truthy not merely an assertion that a truthy value exists in the databaset

user1486133
  • 1,309
  • 3
  • 19
  • 37

2 Answers2

3

If you need old-style JavaScript, then just go for a plain for loop:

function hasTruthy(fruits) {
    for (var key in fruits) {
        if (fruits.hasOwnProperty(key) && fruits[key]) return true;
    }
    return false;
}
trincot
  • 317,000
  • 35
  • 244
  • 286
0

You don't even need that hasOwnProperty check.

var fruits = {
  apples: false,
  oranges: true,
  bananas: true
}


function hasTruthyValue(obj) {
  for (let key in obj) {
    if (obj[key]) {
      return true;
    }
  }
}

console.log(hasTruthyValue(fruits)); //true
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Katie.Sun
  • 711
  • 3
  • 15
  • `var fruits = { apples: false, oranges: false, bananas: false }`returns undefined - which IS falsy, but still – mplungjan Nov 27 '18 at 17:07
  • @mplungjan which isn't a `truthy` value – Katie.Sun Nov 27 '18 at 17:07
  • The OP wrote that `Object.values(fruits).some` worked. Please be aware that the corresponding `for` loop only guarantees the same properties to be used as `Object.values` when it is verified that the listed property is an owned property. – trincot Nov 27 '18 at 17:07
  • https://stackoverflow.com/questions/32422867/when-do-i-need-to-use-hasownproperty – mplungjan Nov 27 '18 at 17:07
  • @trincot the way she posted the code, I would say you don't need the check. if she only wants the original object to return `true`, that would be up for her to decide. Personally, if I wrote `var fruits = { apples: false, oranges: false, bananas: false } fruits.pears = true;` and sent that to a function to check if it had any `truthy` values, I would be confused when it told me it didn't. – Katie.Sun Nov 27 '18 at 17:12
  • Why would it say it didn't? I think we have a misunderstanding here. – trincot Nov 27 '18 at 17:14
  • @trincot you're right. But in that case, when would `hasOwnProperty` ever return false? – Katie.Sun Nov 27 '18 at 17:17
  • It would return false when the property is not an *owned* property, but one somewhere up in the prototype chain. `Object.values` only iterates over owned properties. – trincot Nov 27 '18 at 17:50
  • @trincot but doesn't a `for in` loop iterate over all of the properties? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in#Iterating_over_own_properties_only – Katie.Sun Nov 27 '18 at 18:04
  • I don't do enough object-based writing with JavaScript to quickly create an example that would return `false` for `hasOwnProperty`, but it is less inclusive, no? Like you may have a situation where the value is `true` but it isn't an *own* property? Seems like it could be confusing – Katie.Sun Nov 27 '18 at 18:09
  • See my comment above. I can only repeat that to guarantee that only owned properties are iterated over (as `Object.values` does) it is needed to add the condition in a `for...in` loop. See [When do I need to use hasOwnProperty()](https://stackoverflow.com/questions/32422867/when-do-i-need-to-use-hasownproperty). – trincot Nov 27 '18 at 19:07