Serenity
Serenity

Reputation: 4064

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

Upvotes: 1

Views: 86

Answers (4)

MikaAK
MikaAK

Reputation: 2364

You can use Array.includes with your filter

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:

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

Upvotes: 2

Raphael Rafatpanah
Raphael Rafatpanah

Reputation: 20007

You have an array of objects, which is a great use case for 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/

Upvotes: 1

Leo
Leo

Reputation: 13858

It's not as easy as simple match since you need a comparison of 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]
    }
  });
} 

Upvotes: 1

ibrahim mahrir
ibrahim mahrir

Reputation: 31712

// 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"]));

Upvotes: 2

Related Questions