简体   繁体   English

javascript将数组复制到另一个

[英]javascript copying array to another

i have the following code snippet, where inside for loop the value to contain is not getting assigned, is this is the proper way to copy array to other.?? 我有以下代码片段,其中在for循环内未分配要包含的值,这是将数组复制到其他对象的正确方法吗?

as here 就像这里

  var groupCondition = "ALL-OF-THEM&ALL-OF-THEM&ALL-OF-THEM&ALL-OF-THEM&";
  var groupParam =    "rsTxTraceMsgAside&rsExpTraceMsgAside&rsTxTraceMsgBside&rsExpTraceMsgBside&#hp1TxTraceMsg&hp1ExpTraceMsg&#";



  var grpNameArr = groupParam.split("#");
  var groupcn= groupCondition.split("&");
  var m=grpNameArr.length;

var contain=new Array();
var cmds=new Array();
var ii;

for(ii=0;ii<(m-1);ii++)
{
   contain[ii] = groupCn[ii];
   cmds[ii] = grpNameArr[ii];
 }

If you want to clone an array you can use slice() method as mentioned in this page: 如果要克隆数组,可以使用本页中提到的slice()方法:

http://www.hardcode.nl/subcategory_1/article_414-copy-or-clone-javascript-array-object http://www.hardcode.nl/subcategory_1/article_414-copy-or-clone-javascript-array-object

var oldArray = ["mip", "map", "mop"];
var newArray = oldArray.slice();

your array declaration is wrong , it should be like :- 你的数组声明是错误的,应该像:-

var groupcn=["All","All","All","All"]; 
var grpNameArr=["abc","def","ghi"]; 

So, after your edit, I see your problem was that you has some typo's in your variable names. 因此,在您进行编辑后,我发现您的问题是变量名中有一些错字。

Replace: 更换:

var grpNameArr = groupParm.split("#");
var groupcn= groupCondtn.split("&");

With: 带有:

var grpNameArr = groupParam.split("#");
//                      ^ Missing `a` and `r`.
var groupCn= groupCondition.split("&");
//       ^ Capital C  ^ Missing `i`'s and `o`.

Old Answer 旧答案

These 2 lines: 这2行:

var groupcn = All,All,All,All; 
var grpNameArr = abc,def,ghi;

Are probably your problem. 可能是您的问题。

What you're doing there is assigning the variable All to a new variable groupcn , then declaring All as a new variable, 3 times. 您正在执行的操作是将变量All分配给新变量groupcn ,然后将All声明为新变量3次。

var groupcn=All,
    All, // new variable with the name `All`
    All, // new variable with the name `All`
    All; // new variable with the name `All`. These 3 override `All`

You'll need to initialize them like this: 您需要像这样初始化它们:

var groupcn = [All,All,All,All]; 
var grpNameArr = [abc,def,ghi];

Other than that, assuming m is the length of groupcn , the code should work. 除此之外,假设mgroupcn的长度,代码应该可以工作。

However, a shorter solution is to copy the arrays like this: 但是,一个较短的解决方案是像这样复制数组:

var contain = groupcn.slice();
var cmds = grpNameArr.slice();

you can use : 您可以使用 :

var contain=groupcn.concat();
var cmds=grpNameArray.concat();

Following mistakes were in the code 代码中出现以下错误

  1. Using one loop for both the arrays. 对两个阵列使用一个循环。 Since there length is not same two different loops should be used. 由于长度不一样,应该使用两个不同的循环。

  2. There was typo mistake in groupcn variable. groupcn变量中有拼写错误。

Check this code 检查此代码

<!DOCTYPE html>
<html>
<script>
function chk()
{
  var groupCondition = "ALL-OF-THEM&ALL-OF-THEM&ALL-OF-THEM&ALL-OF-THEM&";
  var groupParam =    "rsTxTraceMsgAside&rsExpTraceMsgAside&rsTxTraceMsgBside&rsExpTraceMsgBside&#hp1TxTraceMsg&hp1ExpTraceMsg&#";



var grpNameArr = groupParam.split("#");
var groupcn= groupCondition.split("&");

var contain=new Array();
var cmds=new Array();
var ii;

for(ii=0;ii<(groupcn.length-1);ii++)
contain[ii] = groupcn[ii];

for(ii=0;ii<(grpNameArr.length-1);ii++)
cmds[ii] = grpNameArr[ii];

alert("groupcn   =  "+contain);
alert("grpNameArr   =  "+cmds);
}
</script>
<body onload="chk()">

</body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM