简体   繁体   English

数组内的过滤器数组

[英]filter array within array

I have an array of products and i want to filter all those products with the level. 我有一系列产品,我想过滤所有这些产品。 The level here is also an array because i have used checkbox so if user selects level 1, a product with level 1 should be shown and if user again selects level 2, a product with level 1 and 2 should be shown. 这里的级别也是一个数组,因为我使用了复选框,因此如果用户选择级别1,则应显示级别1的产品,如果用户再次选择级别2,则应显示级别1和2的产品。 However i am getting all the products instead of the selected level. 但是,我正在获得所有产品,而不是所选级别。

Here is my code 这是我的代码

// sample product data
products = [
{id: "1", sellerLevel: "new", status: "online"},
{id: "1", sellerLevel: "1", status: "offline"},
{id: "1", sellerLevel: "1", status: "offline"},
{id: "1", sellerLevel: "2", status: "online"}
]

function productFilterFromLevel({level}) { // level ["new", "1"]
  return products.filter(function(product) {
    return level.filter(function(lvl) {
      return product.sellerLevel === lvl;
    });
  });
} 

You can use Array.includes with your filter 您可以在过滤器中使用Array.includes

const productFilterFromLevel = ({level}, products) => products
  .filter(item => level.includes(item.sellerLevel))

or without includes you can just filter on the keys like so 不论是否包含,您都可以像这样过滤键

const productFilterFromLevel = function({level}, products) {
  return products.filter(function(product) {
    return level.indexOf(product.sellerLevel) !== -1
  })
}

as you can see the includes version is much cleaner but you can always make your own include function: 如您所见, includes版本更加干净,但是您始终可以创建自己的包含函数:

const arrayIncludes = function(array, item) {
  return array.indexOf(item) !== -1
}

You have an array of objects, which is a great use case for Array.filter . 您有一个对象数组,这是Array.filter的一个很好的用例。

// sample product data
products = [
{id: "1", sellerLevel: "new", status: "online"},
{id: "1", sellerLevel: "1", status: "offline"},
{id: "1", sellerLevel: "1", status: "offline"},
{id: "1", sellerLevel: "2", status: "online"}
]

function filter(products, matches) {
  return products.filter(product => {
    return matches.includes(product.sellerLevel);
  });
}

console.log(filter(products, ['new', '1']));

// [
//   {id: "1", sellerLevel: "new", status: "online"},
//   {id: "1", sellerLevel: "1", status: "offline"},
//   {id: "1", sellerLevel: "1", status: "offline"}
// ]

https://jsfiddle.net/persianturtle/fL8eupoz/3/ https://jsfiddle.net/persianturtle/fL8eupoz/3/

It's not as easy as simple match since you need a comparison of sellerLevel : 这不是简单匹配那么容易,因为您需要比较sellerLevel

function productFilterFromLevel(level) { // level ["new", "1"]
  return products.filter(function(product) {
    for (var i = 0; i < level.length; i++) {
      if (level[i] === 'new') {
        return product.sellerLevel === 'new'
      }
      return +product.sellerLevel <= +level[i]
    }
  });
} 

 // sample product data products = [{ id: "1", sellerLevel: "new", status: "online" }, { id: "1", sellerLevel: "1", status: "offline" }, { id: "1", sellerLevel: "1", status: "offline" }, { id: "1", sellerLevel: "2", status: "online" }] function productFilterFromLevel(level) { return products.filter(function(product) { return level.indexOf(product.sellerLevel) != -1; }); } console.log(productFilterFromLevel(["new", "1"])); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM