View Single Post
Old 01-28-2013, 04:38 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Well, the obvious:
Code:
var s = "id1,name1,id2,name2,id3,name3...."
var temp = s.split(",");
var element = [ ];
for ( var t = 0, e = 0; t < temp.length; t += 2, ++e )
{
    element[e] = [ temp[t], temp[t+1] ];
}
But we could probably get sneakier....

Code:
var s = "id1,name1,id2,name2,id3,name3...."
var temp = s.replace( /([^\,]+\,[^\,]+)\,/g, "$1;" ).split(";");
var element = [];
for ( var t = 0; t < temp.length; ++t )
{
    element[t] = temp[t].split(",");
}
Mildly sneaky. Works so long as none of the substrings contain semicolons.
Seems hardly worth trouble. I'd just use version 1.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote