Reputation: 5614
alert(cellvalue)
shows three seperate pop ups:
-10-|Car|*POB*[,]-20-|Bus|*CLR*
-22-|Car|*CLR*[,]-5-|Bus|*POB*
-12-|Car|*POB*[,]-55-|Bus|*CLR*
I am then splitting these values and getting the three values I need as follows:-
var array = cellvalue.split("[,]");
var start_carStat = array[0].indexOf('*') + 1;
var end_carStat = array[0].indexOf('*',start_carStat);
var text_carStat = array[0].substring(start_carStat,end_carStat);
alert(text_carNum); shows '10', '22', '12'
var start_carNum2 = array[1].indexOf('-') + 1;
var end_carNum2 = array[1].indexOf('-',start_carNum2);
var text_carNum2 = array[1].substring(start_carNum2,end_carNum2);
alert(text_carNum); shows '20', '5', '55'
and then returning them as follows:-
return (text_carNum+', '+text_carNum2);
Since there could be upto 20 values in each array I am trying to use a for loop to achieve the same thing but I can't seem to get it to work.
At the moment I have:-
for(var i=0;i<array.length;i++){
var start_carNum = array[i].indexOf('-') + 1;
var end_carNum = array[i].indexOf('-',start_carNum);
var text_carNum = array[i].substring(start_carNum,end_carNum);
return (text_carNum);
}
with this alert(text_carNum) only shows '10','22','12'.
Any ideas on how I could get this to work the same way as above using for/each?
(hoping I have explained this clear enough)
Upvotes: -1
Views: 63
Reputation: 3890
You call return
in your loop, so the function exits at the end of the first iteration. You have to concatenate all values in a variable :
var str_return = '';
for(var i=0;i<array.length;i++){
var start_carNum = array[i].indexOf('-') + 1;
var end_carNum = array[i].indexOf('-',start_carNum);
var text_carNum = array[i].substring(start_carNum,end_carNum);
str_return += '<span style="color:'+color+' !important;">'+text_carNum +'</span>';
}
return str_return;
BTW, notice that I change the last line : the style
attribute was closed (double quote) just after the color, it has to be closed after !important
.
Upvotes: 2
Reputation: 3120
return
will always exit the function as soon as it is called. In your code, it is only able to reach array[0]
before it leaves the loop.
A solution would be to append the values of array
to another variable within your loop, and then return a value after the loop has finished.
Upvotes: 2