-1

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
asked Aug 10, 2015 at 4:34

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.