JavaScript使用带有数组拆分数组的for循环

时间:2013-10-17 05:09:44

标签: javascript arrays function for-loop

嘿伙计们我需要一些代码来分割一个数组,该数组包含一个字符串,该字符串是一个项目,数量的分隔符是(:)。 (例如,气体:30) 将transArray中的元素加载到项目和金额字段的hmtl texbox的值中 请不要苛刻这些评论这是我的第一语言类型语言。 任何帮助表示赞赏!

var load = function ()
{

   mySetArray();  //Fills the transArray randomly with 1-4 items
   var item = '';
   var amount = '';

   for ( i=1; i<=transArray.length; i++)
   {
    item = 'item' + i;
    amount = 'amount' + i;
    transArray.split(":");      
   }
}

  var mySetArray = function ()
  {

 var myRandom = Math.floor((Math.random() * 100) / 25) + 1;  //a number between 1      and 4

 transArray = new Array();  //Resets the Array to empty

 if (myRandom == 1)
 {
    transArray[0] = "Food:200";
 }

 if (myRandom == 2)
 {
    transArray[0] = "Food:200";
    transArray[1] = "Toys:700";
 }

 if (myRandom == 3)
 {
    transArray[0] = "Food:200";
    transArray[1] = "Toys:700";
    transArray[2] = "Mortgage:1800";
 }

 if (myRandom == 4)
 {
    transArray[0] = "Food:200";
    transArray[1] = "Toys:700";
    transArray[2] = "Mortgage:1800";
    transArray[3] = "Cable:130";
 }
     }

      window.onload = function ()
    {
$("load").onclick = load;
    }

5 个答案:

答案 0 :(得分:0)

拆分数组,例如:

transArray[0] = "Food:200";

只需使用拆分:

var newArray = transArray[0].split(':');
// newArray[0] = 'Food', newArray[1] = '200'

答案 1 :(得分:0)

变化:

for ( i=1; i<=transArray.length; i++) {
    item = 'item' + i;
    amount = 'amount' + i;
    transArray.split(":");      
}

for ( i=1; i<=transArray.length; i++) {
    item = 'item' + i;
    amount = 'amount' + i;
    var splitted = transArray[i].split(":"); <-- split each item in transArray 
    console.log(splitted);
}

答案 2 :(得分:0)

这里transArray是一个数组。您应该使用拆分它的值,即transArray[i].split(":");

所以更新你的代码:

for ( i=1; i<=transArray.length; i++)
   {
      item = 'item' + i;
      amount = 'amount' + i;
      var splittedData = transArray[i].split(":");      
      // It will give Item in 0th index and amount in 1st field. 
   }

答案 3 :(得分:0)

var arr = new Array();
arr[0] = "Gas:200";

var newArr = arr[0].split(':');

答案 4 :(得分:0)

JSFIDDLE DEMO

随时随地调用加载功能(就像我一样)

function load()
{

   transArray = mySetArray();  //Fills the transArray randomly with 1-4 items
   var item = '';
   var amount = '';

   for ( i=0; i<=transArray.length; i++)
   {
    ar = transArray[i].split(":");
    alert((i+1)+" Item="+ar[0] + " Amount="+ ar[1]); // You ca use it in your own way
   }
}
load();

  function mySetArray()
  {

 var myRandom = Math.floor((Math.random() * 100) / 25) + 1;  //a number between 1      and 4

 transArray = new Array();  //Resets the Array to empty

 if (myRandom == 1)
 {
    transArray.push("Food:200");
 }

 if (myRandom == 2)
 {
    transArray.push("Food:200");
    transArray.push("Toys:700");
 }

 if (myRandom == 3)
 {
    transArray.push("Food:200");
    transArray.push("Toys:700");
    transArray.push("Mortgage:1800");
 }

 if (myRandom == 4)
 {
    transArray.push("Food:200");
    transArray.push("Toys:700");
    transArray.push("Mortgage:1800");
    transArray.push("Cable:130");
 }
      return transArray;
}