1

I want to check if a certain property has a certain value using lodash. The code below prints out false. How can I check the value correctly?

code example

const ob = {
 archived:true,
 done: true
}

console.dir(_.includes(ob.archived, true));
vuvu
  • 4,886
  • 12
  • 50
  • 73
  • Possible duplicate of [How do I use the includes method in lodash to check if an object is in the collection?](https://stackoverflow.com/questions/25171143/how-do-i-use-the-includes-method-in-lodash-to-check-if-an-object-is-in-the-colle) – ehacinom May 03 '19 at 15:48

2 Answers2

2

You need to use _.includes directly with the object, here's an example of that:

const ob = {archived:true};

_.includes(ob, true); // true

If you have to check for a single value only you can use this:

const ob = {archived:true, done: true};

_.includes(_.pick(ob, ['archived']), true); // true
Emad Salah
  • 815
  • 1
  • 8
  • 19
0

You can use _.matchesProperty(). The method generates a function, that accepts an object, and returns true if the object contains the stated property and value.

const hasArchivedTrue = _.matchesProperty('archived', true)

const ob = {
 archived: true,
 done: true
}

console.log(hasArchivedTrue(ob)) // true
console.log(hasArchivedTrue({})) // false
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
Ori Drori
  • 183,571
  • 29
  • 224
  • 209