Filter Multiple Condition - Javascript

I’m looking for help for JavaScript code to filter multiple condition for some items, I want to filter

Item: A, Price: 20, Location: USA, Shipping: Free
Item: B, Price: 80, Location: France, Shipping: Free
Item: C, Price: 60, Location: China, Shipping: Free
Item: D, Price: 10, Location: Japan, Shipping: Free
Item: E, Price: 70, Location: China, Shipping: Unspecified
Item: F, Price: 100, Location: USA, Shipping: 20
Item: G, Price: 35, Location: USA, Shipping: Free

I try to make some code, cut still stuck somewhere, I want to filter items by lowest price, free shipping and be located in the USA.

For example in above case the result has to be

Item = “A”, Price = “20”, Location = “USA”, Shipping = “Free”;
Item = “G”, Price = “35”, Location = “USA”, Shipping = “Free”;

This is my JavaScript code, but I think there is an error, especially on how to filter price starting by lowest price. Help me with correct Javascript code to display the result above.

const products = [

{Item: 'A', Price: '20', Location: 'USA', Shipping: 'Free'},
{Item: 'B', Price: '80', Location: 'France', Shipping: 'Free'},
{Item: 'C', Price: '60', Location: 'China', Shipping: 'Free'},
{Item: 'D', Price: '10', Location: 'Japan', Shipping: 'Free'},
{Item: 'E', Price: '70', Location: 'China', Shipping: 'Unspecified’},
{Item: 'F', Price: '100', Location: 'USA', Shipping: '20'},
{Item: 'G', Price: '35', Location: 'USA', Shipping: 'Free'},


const filters = {


      return a.price-b.price


  shipping: shipping => shipping.find(y => ['Free Shipping'].includes(y.toUowerCase()),

  locations: locations => locations.find(x => ['USA'].includes(x.toUpperCase())),




Welcome, mos11.

In order to help you, it would be useful if you gave a lot more information:

  1. What code have you already tried?
  2. How are these items created/stored?
  3. Are your items objects?
  4. How do you want the filter to display the information?

The more information we have, the more we can help.

1 Like

There are all sorts of questions going on in my head here. As @Sky020 has asked, what have you tried, what are you expecting, and what is your code doing (or not doing)? Are these things an array of objects? An array of arrays? A massive object with nested objects as properties? So much we don’t know.

Are you looking to filter by conditions, or filter by some, and sort by some? Or sort by all? May make sense to filter by, say, country, then sort by price, then shipping. Filtering by price seems very constrictive, but it is a possibility.

The more info you can provide, the better we are able to help you.

That said, the idea itself is a fun one. I’m running with it as a thought experiment (for example, how might one code a spreadsheet functionality, with “sort by multiple” or “filter by multiple” or "sort by some, and filter by others). I’ve built a repl that does some of this, it filters by Location, then sorts by Price, but I’m working on abstracting it a bit more and building a blog post about the subject.

It is a higher order of thinking involved. If it’s filtering by more than one, that can be pretty straightforward - simply feed the result of the first filter() call into a second filter(), and so on. For example, if this were an inventory and I did:

inventory.filter(item => item.Location==="USA");

… that would give me a list of inventory items in the USA. If I wanted to filter by that AND free shipping, say, I could do:

         .filter(item=>item.Shipping==="Free"); // this line works on the array returned by the first filter

Sort by multiple options is a little more tricky, but it is do-able. Again, though, without knowing what you’re looking for, I’m just having a fun mental exercise here.

EDIT: This one got me thinking, quite a lot. I was having crazy fun with it. Filtering by multiple criteria isn’t a huge deal - simply pass the first filter into a second filter, and so on. Each time, the list will get pared down.

But but but, what if we wanted to sort by more than one criteria? I found a JSON mock inventory of twenty items, then filtered by Category, and within that filtered list I sorted by Subcategory and within that, sorted by Price. So it ends up with:

 |_ Beef
   |_<$2 beef item>
   |_<$4 beef item>
   |_<$7 beef item>j
|_ Bird
   |_<$3 poultry item>
   |_<$5 poultry item>
|_ Mutton
   |_mutton item

Now, it displays them as the same array, simply ordered by both those sort criteria - but it only runs through sort once!

Sorry, I was having a nerd moment, this was a fun rabbit hole to fall down. If any of you want to play with the code, or to extend it and see where it takes you, feel free: Here’s the repl

Can you be more speciek it’s a bit confusing of what you want to do here excatlly

1 Like

I have edited my question and put my code

I have edited it, I hope it understandable, help me with correct code.

what would this be? it doesn’t follow array syntax nor object syntax

Thank you for correcting me, (It’s part of learning) I have made some edition, please have a look at it,

you wrote the property of the object as Price, casing is important

what should this do?
you are using includes on an array with only one item
and also, what would toUowerCase be? careful with spelling

Could you write the perfect javascript code based on what I wrote?

maybe, but why don’t you try yourself?

There is no such thing as the perfect javascript code beside, what would you learn if others do the coding for you?

So yesterday, I sort of fell down a rabbit hole, and went COMPLETELY off-topic, and for that I apologize. I’ll try to behave today. :wink:

Now, nobody is going to write your code for you, as that runs counter to what FCC is all about. If you are looking to learn to code, then this is the right place. If you are trying to get code written for you, there are services out there that can help with that. We are here to guide and mentor those interested in learning to code.

Your array of objects is now actually objects, which is great. Each of those objects have very specific set of properties: Item is a property name, as is Price, Location, and Shipping. Using those, we have all sorts of options.

Now, you’re talking about the Array.prototype.find() ( function, which is great for finding the first matching item, but Array.filter() ( seems more in line with what you are looking for.

Given your array of objects, we can use products.filter(...) to narrow down the list of products, and can continue to narrow that list with further .filter(...) calls.

The tricky bit is, how do you write what goes inside that .filter(..)?