To clone an array in javascript you can use the slice() method:
var a = [ 'apple', 'orange', 'grape' ];
var b = [];
b = a.slice(0);
b[0] = 'cola';
console.log(a);//[ 'apple', 'orange', 'grape' ]
console.log(b);//[ 'cola', 'orange', 'grape' ]
Resource:
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/slice
http://www.xenoveritas.org/blog/xeno/the-correct-way-to-clone-javascript-arrays
var a = [ 'apple', 'orange', 'grape' ];
var b = [];
b = a.slice(0);
b[0] = 'cola';
console.log(a);//[ 'apple', 'orange', 'grape' ]
console.log(b);//[ 'cola', 'orange', 'grape' ]
Resource:
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/slice
http://www.xenoveritas.org/blog/xeno/the-correct-way-to-clone-javascript-arrays
No comments:
Post a Comment