0

I have got a 3D array and I want to make another copy of it into another array.

Assigning directly would return a string to the new array not a 3D array.

foo = [] //3D array; 
boo = foo //boo becomes a string

Any idea of how to do this?

Edit: this is the code

Background.js

function onRequest(request, sender, sendResponse) {
localStorage = request.mes; // mes is an array
}; 
chrome.extension.onMessage.addListener(onRequest);
Ouerghi Yassine
  • 1,835
  • 7
  • 43
  • 72
  • 2
    A string to the new array? You mean a reference? To copy an array, there are various methods, including `boo = foo.slice();` (=shallow copy). If you want a deep copy, recursively loop through the array), or use `bar = JSON.parse(JSON.stringify(foo));` (note: only works with JSON-serializable objects). Related/duplicate: http://stackoverflow.com/q/10941695 – Rob W Aug 14 '12 at 09:15
  • i dont want the new variable to be string, i want it just the other one to be an array, i tried the slice method, but it works only with 2D arrays @assylias well you can try it, and post your code – Ouerghi Yassine Aug 14 '12 at 09:16
  • @RobW it didnt work ... im using it in google chrome extension though... – Ouerghi Yassine Aug 14 '12 at 09:22
  • That doesn't matter, Chrome extensions do not define special behaviour for arrays. Include your relevant code in the question + the expectations. – Rob W Aug 14 '12 at 09:23
  • When you assign an array to another variable, it definitely **does not** become a string. You must be doing something strange... – Felix Kling Aug 14 '12 at 09:24
  • @yassine_hell Where did you get the idea to use `localStorage` for saving ARRAYS (assuming that you're using `localStorage.foo=..`, and not `localStorage=..`)? `localStorage` can only save strings. If you want to save arrays, use `localStorage.setItem('bar', JSON.stringify(foo));` to save the list, and `JSON.parse(localStorage.getItem('bar');` to retrieve the item. – Rob W Aug 14 '12 at 09:35

2 Answers2

0

You would probably use clone.Try this,

var a = [1,2,[3,4,[5,6]]];

Array.prototype.clone = function() {
    var arr = [];
    for( var i = 0; i < this.length; i++ ) {
//      if( this[i].constructor == this.constructor ) {
        if( this[i].clone ) {
            //recursion
            arr[i] = this[i].clone();
            break;
        }
        arr[i] = this[i];
    }
    return arr;
}

var b = a.clone()

console.log(a);
console.log(b);

b[2][0] = 'a';

console.log(a);
console.log(b);
pdpMathi
  • 777
  • 3
  • 7
0

A solution I've found relies on using jQuery, hopefully that won't be an issue?

var a1 = ['test', ['a','b',1], [[1,2,3],[4,5,6]]];
console.log(a1);
var a2 = jQuery.extend(true, {}, a1);
a1[0] = 'test - changed';
console.log(a1);
console.log(a2);

Fiddle: http://jsfiddle.net/gRoberts/AhKNx/

Simply setting var a2 = a1; simply creates a reference to the original object, resulting in a2[0] being changed to test - changed;

Look at your console (F12 in Firefox/Chrome) to see the output from my fiddle ;)

Hope that helps?

Gavin

Gavin
  • 6,284
  • 5
  • 30
  • 38