There is a product item:

let refrigerator = { price: 840, count: 1 }; 

and object basket:

 let basket = { }; 

How to make the function of adding goods to the basket so that the name of the first object is the key of the second object?

    2 answers 2

     let refrigerator = { price: 840, count: 1 }; let basket = {}; function addToBasket(item) { basket = { ...basket, ...item } } addToBasket({refrigerator}) console.log(basket) 

      The syntax of the extension is used , which will allow to connect the basket with the item object.

       let refrigerator = { price: 840, count: 1 }; // если добавить при объявлении let basket = { refrigerator // сокращение от refrigerator: refrigerator }; let foo = { price: 200, count: 5 }; let bar = { price: 100, count: 2 }; // если добавит через функцию add({ foo, bar }) console.log(basket) function add (goods) { return basket = { ...basket, ...goods } }