创建数组的副本并处理原始数组 [英] Create a duplicate of an array and manipulate the original

查看:71
本文介绍了创建数组的副本并处理原始数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将从对我英语不好的人道歉开始。我会尽量保持清楚。 :)

I will start with an apology about my poor English. I will try to be as clear as I can. :)

我有一个3维数组(只是2维数组的数组)。我的目标是采用二维数组之一,并将其​​逆时针旋转90°。看起来像这样:

I have a 3-dimensional array (just an array of 2-dimensional arrays). My goal is to take one of the 2-dimensional arrays, and rotate it 90° counterclockwise. It look like this:

[1|2|3]  
[4|5|6]
[7|8|9]  

我尝试使它像这样旋转:

I try to make it "rotate" like this:

[3|6|9]  
[2|5|8]  
[1|4|7]

我想更改原始数组,所以我想需要创建一个副本所以我将为您提供参考。所以,这就是我所做的:

I want to change the original array, so I figured that I need to create a copy of it so i will have a reference to work with. So, here is what I have done:

var temp = [];
var cube =  [
    [
        ['A', 'A', 'A'],
        ['A', 'A', 'A'],
        ['A', 'A', 'A']
    ], [
        ['B', 'B', 'B'],
        ['B', 'B', 'B'],
        ['B', 'B', 'B']
    ], [
        ['C', 'C', 'C'],
        ['C', 'C', 'C'],
        ['C', 'C', 'C']
    ], [
        ['D', 'D', 'D'],
        ['D', 'D', 'D'],
        ['D', 'D', 'D']
    ], [
        ['1', '2', '3'],
        ['4', '5', '6'],
        ['7', '8', '9']
    ], [
        ['F', 'F', 'F'],
        ['F', 'F', 'F'],
        ['F', 'F', 'F']
    ]
];

function CCW() {
    temp = temp.concat(cube[4]);
    for(var i = 0; i < 3; i++)
        for(var j = 0; j < 3; j++)
            cube[4][i][j] =  temp[j][2-i];
}

CCW();

原始数组的副本应位于 temp

The copy of the original array should be in temp.

现在,问题出在这一行: cube [4] [i] [j] = temp [j] [2 -i]; 。除了更改 cube 中的数组的值外,它还更改 temp 的值。我试图将 temp = temp.concat(cube [4]); 更改为 temp = cube [4] .slice(0); ,但没有任何区别。

Now, the problem is in this line: cube[4][i][j] = temp[j][2-i];. Instead of changing only the values of the array in cube, it changes also the values of temp. I tried to change temp = temp.concat(cube[4]); to temp = cube[4].slice(0); but it did not make any diffrence.

我该如何解决?谢谢你们。 :)

How can I fix that? Thank you all. :)

推荐答案

直接分配数组时,它是javascript中的引用分配。这意味着任何更改都将反映在两者中。要复制数组,您需要调用array.slice()。

when you assign an array directly, it is a reference assignment in javascript. This means that any changes will be reflected in both. To copy an array, you will need to call array.slice().

注意:这仍然是多维数组上的赋值,因此您需要写一些递归的东西来复制一个具有多个维度的数组(在任何元素上,例如[1、2,['some','inner','array'],3])

note: this will still be an assignment on a multi-dimensional array, so you will need to write something recursive to copy an array that has more than one dimension (at any element, e.g. [1, 2, ['some', 'inner', 'array'], 3])

可以清除这种东西吗?

编辑:这是一个deepCopyArray函数,需要将其扩展为任意对象,不过...

edit: here's a deepCopyArray function, this needs to be exanded for arbitrary objects, though...

function deepCopyArray(arr) {
    var newArr = [];
    for (var i = 0; i < arr.length; i++)
    {
        var a = arr[i], ele;
        if (a instanceof Array) //current element is an array...
            ele = deepCopyArray(a);
        else 
            ele = a;
        newArr.push(ele);
    }
    return newArr;
}

这篇关于创建数组的副本并处理原始数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆