Javascript按值将数组传递给函数,保持原始数组不变 [英] Javascript passing arrays to functions by value, leaving original array unaltered

查看:28
本文介绍了Javascript按值将数组传递给函数,保持原始数组不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里阅读了许多有关按值"和按引用"传递将数组发送到 javascript 函数的答案.但是,我在将数组发送到函数并保持原始数组不变时遇到问题.这个例子说明了这个问题:

I've read many answers here relating to 'by value' and 'by reference' passing for sending arrays to javascript functions. I am however having a problem sending an array to a function and leaving the original array unaltered. This example llustrates the problem:

function myFunction(someArray)
{
// any function that makes an array based on a passed array;
// someArray has two dimensions;
// I've tried copying the passed array to a new array like this (I've also used 'someArray' directly in the code);

funcArray = new Array();
funcArray = someArray;

var i = 0;

    for(i=0; i<funcArray.length; i++)
    {
    funcArray[i].reverse;
    }

return funcArray;

}

我不明白为什么这个函数中的任何东西都应该改变原始数组.

I can't understand why anything in this function should alter the original array.

如果将函数调用分配给新数组,则调用此函数会直接更改原始数组:

calling this function directly changes the original array if the function call is assigned to a new array:

myArray = [["A","B","C"],["D","E","F"],["G","H","I"]];
anotherArray = new Array();

anotherArray = myFunction(myArray);
// myArray gets modified!;

我尝试使用 .valueOf() 发送原语:

I tried using .valueOf() to send the primitive:

anotherArray = myFunction(myArray.valueOf());
// myArray gets modified!;

我什至尝试将数组逐个元素和子元素逐个分解,并将所有内容分配给新的二维数组,而原始数组仍然被修改.

I have even tried breaking the array down element by element and sub-element by sub-element and assigning all to a new 2-d array and the original array still gets modified.

我还将子元素加入一个字符串,处理它们,将它们拆分回数组,原始数组仍然被修改.

I have also joined the sub-elements to a string, processed them, split them back into arrays and the original array still gets modified.

请问,有人知道如何将数组值传递给函数而不更改传递的数组吗?

Please, does any one know how I can pass the array values to a function and not have the passed array change?

推荐答案

在你的函数里面是这样的:

Inside your function there's this:

funcArray = new Array();
funcArray = someArray;

这实际上不会复制 someArray 而是引用它,这就是修改原始数组的原因.

This won't actually copy someArray but instead reference it, which is why the original array is modified.

您可以使用 Array.slice() 创建一个所谓的数组浅拷贝.

You can use Array.slice() to create a so-called shallow copy of the array.

var funcArray = someArray.slice(0);

原始数组将保持不变,其每个元素仍将引用其在原始数组中的对应条目.对于深度克隆",您需要递归地执行此操作;以下问题讨论了最有效的方法:

The original array will be unaltered, but each of its elements would still reference their corresponding entries in the original array. For "deep cloning" you need to do this recursively; the most efficient way is discussed in the following question:

最有效的方法是什么在 JavaScript 中深度克隆对象?

顺便说一句,我在 funcArray 之前添加了 var.这样做使其成为函数的局部变量,而不是全局变量.

Btw, I've added var before funcArray. Doing so makes it local to the function instead of being a global variable.

这篇关于Javascript按值将数组传递给函数,保持原始数组不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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