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
  • 98,904
  • 14
  • 127
  • 179
Are
  • 2,160
  • 1
  • 21
  • 31
  • 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
  • 428,835
  • 81
  • 738
  • 806