views:

56

answers:

6

i have a string coming in like this

138374:order1,738437:order2,927438:order3

etc

I am doing a loop to split orders by comma (,)

but now how to I furthur split it to the order number and oredert name thats is separated by semicolo(:)

Below is the code

function(getOrders) {
  var completeOrderRequest = getOrders.split(',');
  for(var j=0; j < orderRequest.split(',').length; j++) {
    alert(completeOrderRequest[j]);
    var nameOrderIds = completeOrderRequest[j].split(':');

    alert("OrderNumber: "+nameOrderIds[0]);
    alert("Name : "+nameOrderIds[1]);
  }  
}   

Right now its giving me orderNumber 0.
thanks

A: 

oops var nameOrderIds ="";

lol

update it in your question, dont post an an answer
Anurag
A: 
function getOrders( orderString) {
   var orders = orderString.split(','); // create array of ordernumber:name combos
   for( var i=0; i < orders.length; i++) { // use length of above array, not a split again
      var entry = orders[i].split(':'); // now break each combo into it's own array
      alert( "OrderNumber: " + entry[0] );
      alert( "Name: " + entry[1] );
   }
}

demo: http://jsfiddle.net/MDNmF/

Dan Heberden
A: 

Here is a function that should be more like what you're looking for:

function(getOrders) {
  var completeOrderRequest = getOrders.split(',');
  for(var j=0; j < completeOrderRequest.length; j++) {
    var idsAndNames = completeOrderRequest[j].split(':');
    alert("OrderNumber: "+idsAndNames[0]);
    alert("Name : "+idsAndNames[1]);
  } 
}   
thenduks
A: 

Where is the orderRequest in the orderRequest.split(',') coming from?? should this be instead:

for (var j = 0; j < completeOrderRequest.split(',').length; j++)

If so, that means you are doing a split twice, which would not work because you are splitting on the same character.

orvado
A: 

what is orderRequest?

var getOrders = "138374:order1,738437:order2,927438:order3,";

var completeOrderRequest = getOrders.split(",");

for(i = 0; i < completOrderRequest.length; i++){
   var nameOrderIds = completeOrderRequest[j].split(':');
   alert("OrderNumber: "+nameOrderIds[0]);  
   alert("Name : "+nameOrderIds[1]);    
}
KevinDTimm
A: 

Be aware of var hoisting. Javascript doesn't have block-scope variables, only function-scope ones; in your code, nameorderIDs is actually defined just below completeOrderRequest.

DDaviesBrackett