Here is where I want to implement my OR
return bigData.country==["US"||"JP"] && (bigData.description=="iPhone 4S")
[ A
||B||C||... ]&&[ X||Y||Z||....]
As you can see above, I am returning objects, if the value of key of object bigData.country is either US or JP, AND bigData.description is either iPhone 4S, can also be more devices.
I'm able to get the desired result, by
return (bigData.country=="US"||bigData.country=="JP") && (bigData.description=="iPhone 4S")
But as I can have convenience to add and remove from an Array, I am trying to use an array. Suggestion to use something different is also welcomed.
If you want to play around with my code here is REPL
You can use Array.prototype.indexOf (to be != -1) like this:
return ["US", "JP"].indexOf(bigData.country) !== -1 && ["X", "Y", "Z"].indexOf(bigData.description) !== -1;
Or in ES6, you can use Array.prototype.includes like:
return ["US", "JP"].includes(bigData.country) && ["X", "Y", "Z"].includes(bigData.description);
You can use Array#some() method like this:
ES6:
return ['US','JP'].some(val => bigData.country === val) && ['iPhone 4S'].some(v => bigData.description === v);
ES5:
return ['US','JP'].some(function(val){return bigData.country === val}) && ['iPhone 4S'].some(function(v){return bigData.description === v});
Demo:
let bigData = {
country: 'JP',
description: 'iPhone 4S'
};
console.log(['US', 'JP'].some(val => bigData.country === val) && ['iPhone 4S'].some(v => bigData.description === v));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With