🥷Passez au niveau Sup
Avant | Après |
const pizzasWithCheese = [];
for (let i = 0; i < pizzas.length; i++) { if (pizzas[i].ingredients.includes("🧀")) { pizzasWithCheese.push(pizzas[i]); } }
console.log(pizzasWithCheese);
const pizzaWithTomato = [];
for (let i = 0; i < pizzas.length; i++) { if (pizzas[i].ingredients.includes("🍅")) { pizzaWithTomato.push(pizzas[i]); } }
console.log(pizzaWithTomato);
const pizzaWithCactus = [];
for (let i = 0; i < pizzas.length; i++) { if (pizzas[i].ingredients.includes("🌵")) { pizzaWithCactus.push(pizzas[i]); } }
console.log(pizzaWithCactus);
| const hasIng = (ing) => (pizza) => pizza.ingredients.includes(ing);
const chees = pizzas.filter(hasIng("🧀")), tomato = pizzas.filter(hasIng("🍅")), cactus = pizzas.filter(hasIng("🌵"));
|
Code BD
const findToppins = { toppings: { $in: ["cheese", "pepper"] } };
try {
const cursor = await collection.find(findToppins).sort({ name: 1 });
await cursor.forEach((pizza) => {
console.log(`${pizza.name} has "cheese" or "pepper" toppings \n`);
});
} catch (err) {
console.error(
`Something went wrong trying to find the documents: ${err}\n`
);
}
Pour tester le code, vous devez mettre en place une vaiable d'environnement
du type :
mongodb+srv://moz:<password>@cluster0.kkmmj.mongodb.net/?retryWrites=true&w=majority