标签云

微信群

扫码加入我们

WeChat QR Code


If you don't want an object in return then specify [] instead of {}

2018年10月24日28分47秒

Thanks Noah. Looks like my biggest problem was that I was giving $.extend and object as its first argument, not an array.

2018年10月23日28分47秒

Can you explain what is the purpose of b.shift().shift() here?

2018年10月23日28分47秒

b is just being manipulated to show that a and b don't hold the same value. Shouldn't a and b be represented as [[3]]? Shift removes the first value in an array entirely whether it is of any type. It doesn't perform a recursive search for a primitive type and then remove that. The length of an array that holds the context for the method always decreases by 1 and is edited in place.

2018年10月23日28分47秒

Thank you! Your line: var b = $.extend(true, [], a); saved my life! I used $.extend(true, {}, a) braces instead of square brackets!

2018年10月23日28分47秒

thanks it helped very much , normally when we copy array by direct assignment way , when one array is changed then other copied array also get changes , so this method do real copy.

2018年10月23日28分47秒

This will not work for multi dimensional arrays, you have to use "true" as first argument as mentioned in answer by Noah Sussman

2018年10月23日28分47秒

Note that this is not a deep copy.

2018年10月23日28分47秒

similar: var a = [1,2,3]; var b = ([]).concat(a); b is a copy

2018年10月23日28分47秒

Array.slice does not provide a deep copy, which is pretty specifically the question being asked here.

2018年10月23日28分47秒

The author has put deep in parenthesis, so any answer regarding copying is welcome. The text of the actual question doesn't even contain the word deep at all.

2018年10月23日28分47秒

Primitives are not passed by reference. Objects (incl. arrays) are, though.

2018年10月23日28分47秒

Thanks, was looking for a catch-all clone (sometimes have an object, sometimes an array of objects).

2018年10月23日28分47秒

Interesting. Chris West can you say what issues that your answer addressees that jQuery extend does not? Many thanks.

2018年10月23日28分47秒

ADS