问题描述:

This question already has an answer here:

  • Copying array by value in JavaScript

    17 answers

网友答案:

It looks like I was missing the fact that I need to clone the array, not just assign it to a new variable.

It seems like there are many ways to do this, one of which looks like this:

input = [1, 2, 3, 4, 1, 3, 4];

function test(input){
  var newArr = input.slice(0); // clones array
  newArr.splice(0,1);
  return input;
};

console.log(test(input));
网友答案:

In javascript a var that receive an object from another var isn't a copy, but a reference.

http://snook.ca/archives/javascript/javascript_pass

网友答案:

Array assignment does not copy the array. Both newArr and input point to the same object.

相关阅读:
Top