0

I have this code:

const settings = {
            notes: [new musicnote(480,400,"wav/1.wav","cyan",true)],
            emiters: [new emiter(300,240), new emiter(340,240)]
        }

var notes = [];
var emiters = [];

var LoadLevel = function (level) {
    if(settings[level] !== undefined){
        currentLevel = level;
        notes = settings[level].notes;
        emiters = settings[level].emiters;
    }
}

And when I change emiters array in game (delete or add some) it does the same to the settings.emiter array. So my question is - how to pass a new array instead of a reference to array in settings object?

Alexei Levenkov
  • 96,782
  • 12
  • 124
  • 169
Are
  • 2,050
  • 1
  • 21
  • 30
  • Duplicates: http://stackoverflow.com/questions/7486085/copying-array-by-value-in-javascript, http://stackoverflow.com/questions/565430/deep-copying-an-array-using-jquery, and more. – elclanrs Apr 27 '13 at 22:50

1 Answers1

4

You can do that with Array.slice:

var original = [];
var copy = original.slice();

// to test:
copy.push("foo");
alert(original.length); // 0
Jon
  • 413,451
  • 75
  • 717
  • 787