javascript - How do I avoid looping through an array to find a partial match? -
i looping through array of english phrases, , if find match, current text node, replace it's translation in non_english array. of works 100% exact matches.
but partial matches, need use .match command, allows partial matches.
my code search exact matches this:
var found = $.inarray(value,en_lang);
then if there found value, replacement of text. method fast , love it.
however partial word/phrase matching, have use looping code.
// loop thru language arrays (var x = en_count; x > 0; x--) { // assign current from/to variables replace var = en_lang[x]; var = other_lang[x]; // if value match translation if (value.match(from)) { content(node, value.replace(from, to)); } // mark node translated if ($.browser.msie == 'false') { $(node).data('translated', 'yes'); }
}
this job pretty slow. after lot of research, have found can convert english array list-based string via join command.
but unable come function search list partial match, , return position in list.
i trying out old js function created in 2006. can't figure out how position back, correctly.
function listfind(list, value, delimiters) { if (!delimiters) { var delimiters = ',' } _templistsplitarray = list.split(delimiters) var foundidx = 0; (i = 0; < _templistsplitarray.length; i++) { if (_templistsplitarray[i] == value) { foundidx = + 1; break } if (value.match(_templistsplitarray[i])) { foundidx = + 1; break } } return foundidx }
thank time.
javascript has foreach type of system still based on loop
var array = ['hello', 'world']; for(var key in array){ alert(array[key]); }
thats best getting looping though array way allso works objects
var obj = {'one':'hello', 'two':'world']; for(var key in obj){ alert("key: "+key+" value: "+obj[key]); }
updated comments on question can replace text know
var str = "hello world"; str = str.replace("hello", "bye bye"); alert(str);
Comments
Post a Comment