I have a array is product
product[1][...]
product[2][...]
...
And I have a other array is hold.
My command:
hold['product'] = product;
for(i in product){
delete product[i];
}
for(i in hold['product']){
alert(i);
}
And Nothing happen. hold array doesn't have any element when I delete element of product array?
baao
73.6k18 gold badges153 silver badges209 bronze badges
2 Answers 2
That is expected behavior. Complex types such as arrays are passed by reference in JavaScript. So when you assign an array to another variable, you are really assigning the reference. In order to avoid it, you should assign a copy of the original.
Try:
hold['product'] = product.slice(0);
answered Aug 10, 2015 at 4:37
GPicazo
6,7263 gold badges23 silver badges25 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
try this one
var hold['product'] = product.slice();
It will create new copy of array on heap memory.So both array have their own copy
for more please check this one slice
answered Aug 10, 2015 at 4:41
RIYAJ KHAN
15.3k5 gold badges34 silver badges56 bronze badges
Comments
lang-js