2013-10-17 1 views
0

Hey les gars, j'ai besoin d'un code qui va diviser un tableau qui contient une chaîne qui est un élément et un montant avec le délimiteur étant le (:). (par exemple gaz: 30) en chargeant les éléments du transArray dans les valeurs des texboxes hmtl pour les champs item et amount Veuillez ne pas être trop sévère avec les commentaires c'est ma première langue pour un type-language. Toute aide est appréciée!JavaScript utilisant une boucle for avec un tableau de division de tableau

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; 
    } 
+0

Vous pouvez envisager d'utiliser un [littéral d'objet] (https: // developer .mozilla.org/fr-FR/docs/Web/JavaScript/Guide/Valeurs, _variables, _et_literals # Object_literals) au lieu d'un tableau. – thgaskell

+0

Semble deux problèmes majeurs avec votre code. Vous êtes en train de "splitter complete transArray" au lieu de "splitter ses éléments (chaînes) un par un". Aussi, je pense que votre 'mySetArray() ne retourne pas votre transArray pour charger la fonction'. J'ai essayé de fournir une réponse complète avec une démo. – Sami

Répondre

0

diviser un tableau, comme:

transArray[0] = "Food:200"; 

utiliser juste de se séparer:

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

changement:

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); 
} 
0

Ici, transArray est un tableau. Vous devez utiliser la distribution partagée sur ses valeurs à savoir transArray[i].split(":");

jour donc votre code comme ceci:

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. 
    } 
0
var arr = new Array(); 
arr[0] = "Gas:200"; 

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

JSFIDDLE DEMO

fonction de charge d'appel où vous voulez ou il est (comme j'ai done)

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; 
} 
Questions connexes