-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?

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);
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

Comments

Your Answer

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.