I have the following list named cart:
cart = [ {"state": "Canada", "id": "1"}, {"state": "USA", "id": "1"} ]
I have tried filtering it according to the following code where state should either be (USA, Canada), but its giving errors:
var oddNo = _.filter(cart, {'state': {"Canada", "USA"}, 'id': "1"});
console.log(oddNo);
What would be the correct way of doing it?
CodePudding user response:
i'm guessing this is loadash or underscore? the easiest way will be to pass a function as the second parameter that filters the array.
_.filter(cart, (items) => {return item.state === 'USA' || item.state ==='Canada'})
check out the docs on lodash https://lodash.com/docs/4.17.15#filter
although really you probably dont need lodash for this and can just use native js filter https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter