Lodash to find if object property exists in array
arrays, javascript, lodash, reactjs
Solution
This is what worked for me (after testing out the different solutions):
addItem(items, item) {
let foundObject = _.find(items, function(e) {
return e.value === item.value;
});
if(!foundObject) {
items.push(item);
}
return items;
}
Problem
I have an array of objects like this: ``` [ {"name": "apple", "id": "apple_0"}, {"name": "dog", "id": "dog_1"}, {"name": "cat", "id": "cat_2"} ] ``` I want to insert another element, also named `apple`, however, because I don't want duplicates in there, how can I use lodash to see if there already is an object in the array with that same name?