0

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

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;
    }
4

5 回答 5

0

JSFIDDLE 演示

在您想要的任何地方或按原样调用加载函数(正如我所做的那样)

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;
}
于 2013-10-17T06:28:12.193 回答
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. 
   }
于 2013-10-17T05:25:52.573 回答
0
var arr = new Array();
arr[0] = "Gas:200";

var newArr = arr[0].split(':');
于 2013-10-17T05:58:43.897 回答
0

拆分数组,例如:

transArray[0] = "Food:200";

只需使用拆分:

var newArray = transArray[0].split(':');
// newArray[0] = 'Food', newArray[1] = '200'
于 2013-10-17T05:16:45.423 回答
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);
}
于 2013-10-17T05:20:05.697 回答