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?
const ob = {
archived:true,
done: true
}
console.dir(_.includes(ob.archived, true));
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?
const ob = {
archived:true,
done: true
}
console.dir(_.includes(ob.archived, true));
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
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>