I'm creating products as a object in jquery and putting them into an array, but the result is two identical. It easy to understand if I just put an example.
var product = {
id: 0,
name: 'Tomato',
price: 500
};
var array_products = new Array();
//each loops 2
$.each(data, function(i, item) {
var new_product = product;
new_product.id = i;
new_product.name = "new Tomato " + i;
new_product.price = 700;
array_products.push(new_product);
});
console.log(array_products);
Result:
0: {
id: 1,
name: "new Tomato 1",
price: 700
}
1: {
id: 1,
name: "new Tomato 1",
price: 700
}
The problem here is that in the second loop when I "create" the new product, replace the values in the array at the position 0. The result is two equal products instead of two different. What am I doing wrong?
map()is made for translations like this.