Reputation: 7299
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
Upvotes: 3
Views: 2937
Reputation: 42109
Using Array.prototype.includes
is most likely what you're after:
let valid = {
countries: ['US', 'JP'],
description: 'iPhone 4S'
};
let dataset = [{
country: 'US',
description: 'iPhone 4S',
expected: true
}, {
country: 'JP',
description: 'iPhone 4S',
expected: true
}, {
country: 'AA',
description: 'iPhone 4S',
expected: false
}, {
country: 'US',
description: 'iPhone',
expected: false
}]
// iterate over data values
dataset.forEach(data => {
let is_valid = valid.countries.includes(data.country) && valid.description == data.description;
console.log(
`Country:"${data.country}"`,
`Description:"${data.description}"`,
`Expected:${data.expected}`,
`Got:${is_valid}`
);
});
Upvotes: 1
Reputation: 106920
With an Array:
return ['US','JP'].indexOf(bigData.country) >= 0 &&
['iPhone 4S'].indexOf(bigData.description) >= 0;
With an object (possibly better performance):
return {'US': true,'JP': true}[bigData.country] &&
{'iPhone 4S': true}[bigData.description];
As pointed out by @ibrahim mahrir, the object approach doesn't always work. For example, if bigData.country=='toString'
. This is because Javascript objects come with a default baggage of methods from their default Object
prototype. In a real world scenario the likelihood of running afoul of this problem is negligible, but if you want to do it really really properly, there are two options:
1 - Use hasOwnProperty()
:
return {'US': true,'JP': true}.hasOwnProperty(bigData.country) &&
{'iPhone 4S': true}.hasOwnProperty(bigData.description);
2 - Use an object WITHOUT a prototype. This is more verbose to set up, but also more convenient to use. You can also write a trivial method that converts a regular object to a prototype-less object.
var countries = new Object(null);
countries['US'] = true;
countries['JP'] = true;
var descriptions = new Object(null);
descriptions['iPhone 4S'] = true;
return countries[bigData.country] &&
descriptions[bigData.description];
Upvotes: 2
Reputation: 386654
You could use an object as data structure for testing:
var countries = {
US: true,
JP: true
};
return countries[bigData.country] && ...
Upvotes: 1
Reputation: 32145
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));
Upvotes: 2
Reputation: 191996
const countriesToKeep = new Set(['GP', 'US']);
console.log(countriesToKeep.has('UK'));
console.log(countriesToKeep.has('GP'));
Upvotes: 1
Reputation: 31692
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);
Upvotes: 3