// pizzas.mjs
const pizzas = [
{ name: "queen", ingredients: ["🐷", "🍄", "🍅", "🧀"] },
{ name: "cheese", ingredients: ["🧀", "🍅"] },
{ name: "oriental", ingredients: ["🍅", "🐑", "🍄", "🌶"] },
{ name: "royal", ingredients: ["🍅", "🌵"] },
];
export default pizzas;
🥘Fichier test.mjs
import pizzas from "./pizzas.mjs"
function myForEach(array, fx) {
for (let i = 0; i < array.length; i++) fx(array[i], i, array);
}
myForEach(pizzas, (pizza) => {
console.log(pizza);
});
function printPizzaDetails(pizza, index) {
console.log(`Pizza ${index + 1}:`);
console.log(`Name: ${pizza.name}`);
console.log(`Ingredients: ${pizza.ingredients.join(", ")}`);
}
myForEach(pizzas, printPizzaDetails);
let totalIngredients = 0;
function countIngredients(pizza) {
totalIngredients += pizza.ingredients.length;
}
myForEach(pizzas, countIngredients);
console.log(`Total ingredients: ${totalIngredients}`);
let uniqueIngredients = [];
function isUniqueIngredient(ingredient) {
if (uniqueIngredients.includes(ingredient)) {
return
}
uniqueIngredients.push(ingredient);
}
function collectUniqueIngredients(pizza) {
myForEach(pizza.ingredients, isUniqueIngredient);
}
myForEach(pizzas, collectUniqueIngredients);
console.log(`Total unique ingredients: ${uniqueIngredients.length}`);
console.log(`Unique ingredients: ${uniqueIngredients.join(", ")}`);