-1

I am have an array:

var data = [
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0]
];

And if I run my code this array changes to something like:

var data = [
    [1, 0, 0, 0, 0],
    [1, 1, 0, 0, 0],
    [1, 1, 1, 0, 0]
];

And my question: How to save or copy this new array?

pimvdb
  • 151,816
  • 78
  • 307
  • 352
uvyre
  • 23
  • 3

1 Answers1

3

There was very detailed dicussion about related topic. You can found here details: How do you clone an Array of Objects in Javascript?

And probably the most clear answer you can found here What is the most efficient way to deep clone an object in JavaScript? from John Resig

About saving issue - more details needed where exactly you want to save it.

Print data to <textarea>

script

var data = [
                [1, 0, 0, 0, 0],
                [1, 1, 0, 0, 0],
                [1, 1, 1, 0, 0]
            ];

$(document).ready(function () {
    var printData = '';
    $.each(data, function (index, value) {
        printData += '[';
        $.each(value, function (index, value) {
            printData += value + ','
        });
        printData += ']\r\n';
    });

    $('#console').val(printData);
});

html

<textarea id="console"></textarea>
Community
  • 1
  • 1
Samich
  • 29,157
  • 6
  • 68
  • 77