How to do an $in query on the client without minimongo?

Is there anything from lodash that would help here?

I have an array of objects. I have an array of ids. I’d like to search the array of objects by id (by my array of ids).

This should do it :slight_smile:

var ids = [1,2,3], collection = [{id:1},{id:5}];
var matches = _.filter(collection, function(item){
  return _.contains(ids,item.id);
});
console.log(matches);

hmm… says contains is not a lodash function

it used id not _id, for some context

let products = this.state.products;
let selectedProducts = this.state.selectedProducts
let matches = _.filter(products, (item) => _.contains(selectedProducts, item.id))
console.log(matches)

I can’t figure out why this is not working…

let products = this.state.products;
    let selectedProducts = this.state.selectedProducts
    selectedProducts.forEach(id => {
      let productMatch = _.find(products, { id });
      console.log(productMatch);
    })

this.state.selectedProducts.forEach(id => {
let productMatch = _.find(this.state.products, ‘id’, id)
console.log(productMatch)
});

Oh, in Underscore it’s called contains, but in Lodash the renamed it
to includes, sorry.

ES6 also has an array method .includes() you can use instead of relying on Lodash or Underscore

1 Like