简体   繁体   中英

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.??

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:

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:

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.

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.

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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