Learn Basic OOP by Building a Shopping Cart - Step 9

Tell us what’s happening: Hello , I don’t understand what is the destructuration in javascript

`const cartContainer = document.getElementById(“cart-container”);

const productsContainer = document.getElementById(“products-container”);
const dessertCards = document.getElementById(“dessert-card-container”);
const cartBtn = document.getElementById(“cart-btn”);
const clearCartBtn = document.getElementById(“clear-cart-btn”);
const totalNumberOfItems = document.getElementById(“total-items”);
const cartSubTotal = document.getElementById(“subtotal”);
const cartTaxes = document.getElementById(“taxes”);
const cartTotal = document.getElementById(“total”);
const showHideCartSpan = document.getElementById(“show-hide-cart”);
let isCartShowing = false;

const products = [
{
id: 1,
name: “Vanilla Cupcakes (6 Pack)”,
price: 12.99,
category: “Cupcake”,
},
{
id: 2,
name: “French Macaron”,
price: 3.99,
category: “Macaron”,
},
{
id: 3,
name: “Pumpkin Cupcake”,
price: 3.99,
category: “Cupcake”,
},
{
id: 4,
name: “Chocolate Cupcake”,
price: 5.99,
category: “Cupcake”,
},
{
id: 5,
name: “Chocolate Pretzels (4 Pack)”,
price: 10.99,
category: “Pretzel”,
},
{
id: 6,
name: “Strawberry Ice Cream”,
price: 2.99,
category: “Ice Cream”,
},
{
id: 7,
name: “Chocolate Macarons (4 Pack)”,
price: 9.99,
category: “Macaron”,
},
{
id: 8,
name: “Strawberry Pretzel”,
price: 4.99,
category: “Pretzel”,
},
{
id: 9,
name: “Butter Pecan Ice Cream”,
price: 2.99,
category: “Ice Cream”,
},
{
id: 10,
name: “Rocky Road Ice Cream”,
price: 2.99,
category: “Ice Cream”,
},
{
id: 11,
name: “Vanilla Macarons (5 Pack)”,
price: 11.99,
category: “Macaron”,
},
{
id: 12,
name: “Lemon Cupcakes (4 Pack)”,
price: 12.99,
category: “Cupcake”,
},
];

products.forEach(
(element) => {element.id, element.name, element.price, element.category}
);`

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36

Challenge Information:

Learn Basic OOP by Building a Shopping Cart - Step 9

Hello!

it basically means extracting parts of an object or array that you need and using them, in this case as arguments for the callback function.

Example:

const fooArray = [ Hi, and, Hello, I am, from, the, array, called, fooArray]


fooArray.forEach( {Hello, I am, from, fooArray} ) => { }

Watch the syntax: You have to turn the properties you are extracting into an object.

Hello, I am doing like you but it doesn’t work.

Show us the new code please.

const cartContainer = document.getElementById("cart-container");
const productsContainer = document.getElementById("products-container");
const dessertCards = document.getElementById("dessert-card-container");
const cartBtn = document.getElementById("cart-btn");
const clearCartBtn = document.getElementById("clear-cart-btn");
const totalNumberOfItems = document.getElementById("total-items");
const cartSubTotal = document.getElementById("subtotal");
const cartTaxes = document.getElementById("taxes");
const cartTotal = document.getElementById("total");
const showHideCartSpan = document.getElementById("show-hide-cart");
let isCartShowing = false;

const products = [
  {
    id: 1,
    name: "Vanilla Cupcakes (6 Pack)",
    price: 12.99,
    category: "Cupcake",
  },
  {
    id: 2,
    name: "French Macaron",
    price: 3.99,
    category: "Macaron",
  },
  {
    id: 3,
    name: "Pumpkin Cupcake",
    price: 3.99,
    category: "Cupcake",
  },
  {
    id: 4,
    name: "Chocolate Cupcake",
    price: 5.99,
    category: "Cupcake",
  },
  {
    id: 5,
    name: "Chocolate Pretzels (4 Pack)",
    price: 10.99,
    category: "Pretzel",
  },
  {
    id: 6,
    name: "Strawberry Ice Cream",
    price: 2.99,
    category: "Ice Cream",
  },
  {
    id: 7,
    name: "Chocolate Macarons (4 Pack)",
    price: 9.99,
    category: "Macaron",
  },
  {
    id: 8,
    name: "Strawberry Pretzel",
    price: 4.99,
    category: "Pretzel",
  },
  {
    id: 9,
    name: "Butter Pecan Ice Cream",
    price: 2.99,
    category: "Ice Cream",
  },
  {
    id: 10,
    name: "Rocky Road Ice Cream",
    price: 2.99,
    category: "Ice Cream",
  },
  {
    id: 11,
    name: "Vanilla Macarons (5 Pack)",
    price: 11.99,
    category: "Macaron",
  },
  {
    id: 12,
    name: "Lemon Cupcakes (4 Pack)",
    price: 12.99,
    category: "Cupcake",
  },
];

products.forEach(
  ({id, name, price, category}) => {}
);

I solved the issue thank you for your help.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.