1

I just started using nodeJS and I am having some difficulties understanding the variable scope and referencing. For example in the code below, variable a will change/be overwritten even though the slice was made to variable b. My question is, how could I copy variable a into variable b without referencing it/overwriting variable a.

var a = ['a', 'b', 'c', 'd'];
var b = a;
b.splice(3,1);

console.log(a)    //will display ['a', 'b', 'c'] instead of ['a', 'b', 'c', 'd']
jfriend00
  • 683,504
  • 96
  • 985
  • 979
feargreen
  • 35
  • 1
  • 2
  • 1
    possible duplicate of [Copying array by value in javascript](http://stackoverflow.com/questions/7486085/copying-array-by-value-in-javascript) and http://stackoverflow.com/questions/15761565/a-strange-thing-with-js-variables/15761907#15761907 and http://stackoverflow.com/questions/14557654/pointer-behavior-between-objects/14557727#14557727 and http://stackoverflow.com/questions/3978492/javascript-fastest-way-to-duplicate-an-array-slice-vs-for-loop – jfriend00 Jun 05 '15 at 21:39

4 Answers4

1

Instead of using a library like Amit suggested, this can be done natively and without installing a huge library...

Using .slice() with no arguments

var a = ['a', 'b', 'c', 'd'];
var b = a.slice();
b.splice(3,1);

console.log(a);

When you set b = a, you are NOT creating a new array, you are only telling b to hold a reference to a. So a and b are actually referencing the same location in memory. .slice works by returning a entire new array.

Arian Faurtosh
  • 17,987
  • 21
  • 77
  • 115
0

you could use slice

b = a.slice(0);

or you could use concat

b = [].slice(0);

for object there is also the extend method for objects.

and the clone method in underscore library

cs04iz1
  • 1,737
  • 1
  • 17
  • 30
0

The functionality you're looking for is know as deep clone or deep copy.

There are several ways to do this, for example you could use lodash:

var _ = require('lodash');
var a = ['a', 'b', 'c', 'd'];
var b = _.cloneDeep(a);
Amit
  • 45,440
  • 9
  • 78
  • 110
  • 1
    Yeahiii! -1 spree!!! Best when done without a comment – Amit Jun 05 '15 at 21:50
  • You're answer is overkill... why have him download, install and use lodash, when this can simply be done with native Javascript. `_.cloneDeep` is meant for objects, which are a little trickier to clone. – Arian Faurtosh Jun 06 '15 at 00:32
  • @ArianFaurtosh - The question is about general variables. The question was closed as dup referencing how to copy an array. It will not be long before the asking user find that an array copy doesn't work (`a = [{name: 'some name'}]; b = a.slice(); b[0].name = 'some other name'; console.log(a);`). My answer is an overkill for the sample code, not for the question itself. – Amit Jun 06 '15 at 07:06
0

Thanks for the help but I found the answer to my problem. If anyone comes by this post and has the same problem, you can find the solution here Nodejs: how to clone a object

Community
  • 1
  • 1
feargreen
  • 35
  • 1
  • 2