Complementando a resposta p.s.w.g, aqui estão três outras maneiras de conseguir isso usando lodash 4.17.5, sem usar _.includes() :
Digamos que você queira adicionar um objeto entrya uma matriz de objetos numbers, apenas se entryainda não existir.
let numbers = [
{ to: 1, from: 2 },
{ to: 3, from: 4 },
{ to: 5, from: 6 },
{ to: 7, from: 8 },
{ to: 1, from: 2 } // intentionally added duplicate
];
let entry = { to: 1, from: 2 };
/*
* 1. This will return the *index of the first* element that matches:
*/
_.findIndex(numbers, (o) => { return _.isMatch(o, entry) });
// output: 0
/*
* 2. This will return the entry that matches. Even if the entry exists
* multiple time, it is only returned once.
*/
_.find(numbers, (o) => { return _.isMatch(o, entry) });
// output: {to: 1, from: 2}
/*
* 3. This will return an array of objects containing all the matches.
* If an entry exists multiple times, if is returned multiple times.
*/
_.filter(numbers, _.matches(entry));
// output: [{to: 1, from: 2}, {to: 1, from: 2}]
Se você quiser retornar um Boolean, no primeiro caso, poderá verificar o índice que está sendo retornado:
_.findIndex(numbers, (o) => { return _.isMatch(o, entry) }) > -1;
// output: true
_.containsfoi removido no lodash v4 - em_.includesvez disso, use #