javascript - 过滤数组并比较但跳过空值

标签 javascript arrays filter conditional-statements

我目前正在尝试根据他们选择的选项过滤可用产品。

const products = [{
        id: 1,
        name: 'Safari',
        horsepowers: 30,
        doors: 4,
        gear_type: 'automatic',
        wheels: 6
    },
    {
        id: 2,
        name: 'Jungle',
        horsepowers: 50,
        doors: 3,
        gear_type: 'automatic',
        wheels: 5
    },
    {
        id: 3,
        name: 'Moon',
        horsepowers: 30,
        doors: 4,
        gear_type: 'manual',
        wheels: 4
    }
]
const selectedOptions = 
{
   horsepowers: 50,
   doors: 3,
   gear_type: null,
   wheels: null
}

通常我会做类似的事情

const availableProducts = products.filter((product) => 
  product.horsepowers === selectedOptions.horsepowers &&
  product.doors === selectedOptions.doors .... etc

但是,如果用户尚未选择所有可能的选项,我该如何跳过空值、空数组和未定义的值?

最佳答案

下一个提供的方法利用了 the 2nd thisArg argument of almost every available prototypal array method .

因此可以编写一个通用的过滤器函数,它将任何项目的属性值与 selectedOptions 对象配置的相关值进行比较,该对象将作为 filter 与过滤器函数一起传递的第二个参数和作为过滤器函数的 this context ...

const selectedOptions = {
  horsepowers: 50,
  doors: 3,
  gear_type: null,
  wheels: null,
};
const products = [{
  id: 1,
  name: 'Safari',
  horsepowers: 30,
  doors: 4,
  gear_type: 'automatic',
  wheels: 6,
}, {
  id: 2,
  name: 'Jungle',
  horsepowers: 50,
  doors: 3,
  gear_type: 'automatic',
  wheels: 5,
}, {
  id: 3,
  name: 'Moon',
  horsepowers: 30,
  doors: 4,
  gear_type: 'manual',
  wheels: 4,
}];

function doItemPropertiesEqualEveryBoundSelectedOption(item) {
  return Object
    // create key value pairs from the `this` bound selected options.
    .entries(this)
    // skip/ignore selected option entries where `value` equals `null`.
    .filter(([key, value]) => value !== null)
    // execute item specific selected option validation via `every`.
    .every(([key, value]) => item[key] === value);    
}
console.log(
  products
    .filter(
      doItemPropertiesEqualEveryBoundSelectedOption,
      selectedOptions,
    )
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

为了回答 OP 的另一个问题......

"however, how do I skip null values, empty arrays, and undefined values if the user has not yet selected all possible options yet?"

... 并且还提供了一个通用的解决方案,上面的方法可以改成一个thisArg对象,它不仅具有选择的选项,而且还具有不被验证的条件(无效) selectedOption 属性 ...

const products = [{
  id: 1,
  name: 'Safari',
  horsepowers: 30,
  doors: 4,
  gear_type: 'automatic',
  wheels: 6,
}, {
  id: 2,
  name: 'Jungle',
  horsepowers: 50,
  doors: 3,
  gear_type: 'automatic',
  wheels: 5,
}, {
  id: 3,
  name: 'Moon',
  horsepowers: 30,
  doors: 4,
  gear_type: 'manual',
  wheels: 4,
}];

const selectedOptions = {
  horsepowers: 50,
  doors: 3,
  gear_type: null,
  wheels: null,
};
const isInvalidValue = (value) => {
  return Array.isArray(value)
    // empty array validation.
    ? (value.length === 0)
    // undefined and null value validation.
    : (value == null)
}

function doItemPropertiesEqualEveryBoundValidOption(item) {
  const { options, isInvalidValue } = this;
  return Object
    .entries(options)
    .filter(([key, value]) => !isInvalidValue(value))
    .every(([key, value]) => item[key] === value);    
}
console.log(
  products
    .filter(
      doItemPropertiesEqualEveryBoundValidOption,
      { options: selectedOptions, isInvalidValue },
    )
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

关于javascript - 过滤数组并比较但跳过空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69980883/

相关文章:

javascript - 在 angularjs 中加载 json

javascript - 确定是否在城市内或附近

c++ - 将数组传递给函数时遇到问题

javascript - 向filterred div添加属性

javascript - 根据 Javascript 中 for 循环中生成的值过滤对象

javascript - asp.net javascript - 更改更改下拉选择时的标签文本

Javascript - 对象状态

javascript - 使用javascript加入两个数据集

c++ - 提供意外输出的基本 int 数组

r - 使用 %in% 通过列表过滤多列并在 R 中过滤