1

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

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

This worked on my case. Thanks for your help. I forgot indexOf to check in the elements in an array.
2

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
}

Comments

1

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

Comments

1

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/

2 Comments

Thanks for answering. However the problem that i have raised in my question is i am not comparing sellerLevel to 1 or new but with level array not a single value.
@Apprentice, Updated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.