Lodash to find if object property exists in array
You can use Lodash _.find()
like this.
var data = [ {"name": "apple", "id": "apple_0"},
{"name": "dog", "id": "dog_1"},
{"name": "cat", "id": "cat_2"}
]
if(!_.find(data, {name: 'apple'})) {
data.push({name: 'apple2'});
}
console.log(data)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
Reference documentation: https://lodash.com/docs/4.17.14#find
This is Form
_.has(object, path)
Example:
const countries = { country: { name: 'Venezuela' } }
const isExist = _.has(countries, 'country.name')
// isExist = true
For more information Document Lodash