I have cart and products global variable.
Products can have multiple attributes.
Here is a code
var products = [
{
name: 'Table',
price: 200,
attributes: [
{
name: 'Height',
type: 'text',
default_value: '',
},
{
name: 'Width',
type: 'text',
default_value: '',
}
],
},
{
name: 'Chair',
price: 150,
attributes: [
{
name: 'Height',
type: 'text',
default_value: '',
},
{
name: 'Width',
type: 'text',
default_value: '',
},
{
name: 'Company',
type: 'text',
default_value: ''
}
],
}
];
var cart = {
products: [],
};
//console.log('Initial cart',cart);
//add product to cart
let p = Object.assign({},products[0]);
cart.products.push(p);
//console.log('First cart', cart);
//change price
cart.products[0].price = 20;
//console.log('products',products);
//console.log('second cart',cart);
//change attribute of product
cart.products[0].attributes[0].value = 5;
This code changes global products value attributes instead of cart attributes.
Please help me to solve this issue.
lodash.cloneDeep. you are copying only the keys, and each none primitive value (e.g object) is passed by pointer (and not cloned). clone deep is one option, another option, which also do the jobp = JSON.parse(JSON.stringify(products[0]))