Hey there geniuses of SO!
This is for an autocomplete plugin that needs to accept data as an array of arrays and convert it using a format string (or regex). The format string could be any format.
var dataArray = [ ["data1-1", "data1-2", "data1-3"], ["data2-1", "data2-2", "data2-3"],... ];
var format = "<li>{0} <br /> -- <small> {1}, {2}</small></li>";
// alternate formats could be:
// "<li>{0}</li>"
// "<a href="{0}" title="{2}">{1} ({2})</a>"
// etc...
function fillAutocomplete(datum,format){
// do some magic here...
// return "<li>data1-1 <br /> -- <small> data1-2, data1-3</small></li>";
}
The following idea works..but i'd like to see if anything would be faster...
var datum = data[0],
html="<li>\{0\} <br /> -- <small> \{1\}, \{2\}</small></li>";
for(var i=0,l=datum.length;i<l;++i){
var reg = new RegExp("\\{"+i+"\\}");
html=html.replace(reg,datum[i]);
}
I'm open to new ideas on how to approach this problem.