Express Query Practice

const inventoryItems = [
    {
        name: "banana",
        type: "food",
        price: 200,
    },{
        name: "pants",
        type: "clothing",
        price: 2500,
    },{
        name: "basket ball",
        type: "toy",
        price: 1000,
    },{
        name: "rockem sockem robots",
        type: "toy",
        price: 1500,
    },{
        name: "shirt",
        type: "clothing",
        price: 800,
    },{
        name: "soup",
        type: "food",
        price: 300,
    },{
        name: "flour",
        type: "food",
        price: 100,
    }
]

Use this array and write ONE query where the user can recieve filtered itmes based on type

Write another route where an API user can filter by a maxium price AND a minium price. You can make the maxium default to 1000000 and the minimum defualt to 0

EXTRA CREDIT:

Consolidate the two end points you've already written.

If you are stuck:

USE THE LINES OF CODE BELOW AFTER ATTEMPTING TO WRITE THE CODE YOURSELF.

This doesn't give you all the answers, but may help you on your way.

app.get("/", (req, res)=>{
    console.log(req.query);
});

A really good method to filter out all of the items you need is the filter method.

MORE SPOILERS BELOW!! If you scroll down, you agree that a bit of struggle is good for the learning process.
Attempting problems on your own helps you with:

  • Confidence in your ability when you get it on you own
  • Getting feedback from your code for the part that you understand yourself, and then seeing what you might on done wrong when you see how you could have done it
  • Increasing your understanding of the different parts of the problem if all you were able to do on your own was think about what the exercise was asking for, and think through how you might do it
const filteredItems = __________.______(_____ => {
    ______ ____.____ === ___.____.___
})

There's a lot of blanks there, fill them in with AT LEAST educated guesses, and then compare your version

const filteredItems = inventoryItems.filter(___ => {
    ______ item.type === req.query.type
})

If this is challenging to you, you may want to consider redoing the array iterator methods .map(), .find(), .filter(), .some(), .every(), and any others.

These methods have a lot in common. If you're getting better at one, you're getting better at them all.