数组中的逻辑或

Logical OR in an Array

这是我要实现我的 OR

的地方
return bigData.country==["US"||"JP"] && (bigData.description=="iPhone 4S")

[ A || B || C || ... ] && [ X || Y || Z ||....]

正如你在上面看到的,我正在返回对象,如果对象 bigData.country 的键值是 USJPAND bigData.description既可以是iPhone 4S,也可以是多设备

我能够通过

获得想要的结果
return (bigData.country=="US"||bigData.country=="JP") && (bigData.description=="iPhone 4S")

但是因为我可以很方便地从数组中添加和删除,所以我正在尝试使用数组。也欢迎使用不同的建议。

如果你想玩我的代码这里是 REPL

您可以像这样使用 Array.prototype.indexOf(成为 != -1):

return ["US", "JP"].indexOf(bigData.country) !== -1 && ["X", "Y", "Z"].indexOf(bigData.description) !== -1;

或者在 ES6 中,你可以像这样使用 Array.prototype.includes

return ["US", "JP"].includes(bigData.country) && ["X", "Y", "Z"].includes(bigData.description);

使用 ES6 Set (REPL):

const countriesToKeep = new Set(['GP', 'US']);

console.log(countriesToKeep.has('UK'));

console.log(countriesToKeep.has('GP'));

使用数组:

return ['US','JP'].indexOf(bigData.country) >= 0 &&
       ['iPhone 4S'].indexOf(bigData.description) >= 0;

有对象(可能性能更好):

return {'US': true,'JP': true}[bigData.country] &&
       {'iPhone 4S': true}[bigData.description];

正如 @ibrahim mahrir 所指出的,对象方法并不总是有效。例如,如果 bigData.country=='toString'。这是因为 Javascript 对象带有来自其默认 Object 原型的默认方法包袱。在现实世界中,运行 与此问题发生冲突的可能性可以忽略不计,但如果您想真正正确地做到这一点,则有两种选择:

1 - 使用 hasOwnProperty():

return {'US': true,'JP': true}.hasOwnProperty(bigData.country) &&
       {'iPhone 4S': true}.hasOwnProperty(bigData.description);

2 - 使用没有原型的对象。这设置起来更冗长,但使用起来也更方便。您还可以编写一个简单的方法,将常规对象转换为无原型对象。

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];

使用 Array.prototype.includes 很可能是您想要的:

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}`
  );
});

您可以像这样使用 Array#some() method

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});

演示:

let bigData = {
  country: 'JP',
  description: 'iPhone 4S'
};

console.log(['US', 'JP'].some(val => bigData.country === val) && ['iPhone 4S'].some(v => bigData.description === v));

您可以使用对象作为数据结构进行测试:

var countries = {
        US: true,
        JP: true
    };


return countries[bigData.country] && ...