JavaScript – Find and replace word in array

javascript, jquery

How would I find a word (in this case a placeholder, e.g _ORGAN_) in an array and replace it with an element's value?

sql = new Array();$('#system').change(function(){    filter = " topography_index = _ORGAN_";         sql.push(filter);});

In this case I would want to replace _ORGAN_ with $('#organ_menu').val();

Best Solution

Try this:

// sql arrayvar sql = ['organ not found', '_ORGAN_ is here'];var val_to_replace = '_ORGAN_';var replace_with = 'heart'; // temp value - change it with $('#organ_menu').val()$.each(sql, function (key, val) {    // search for value and replace it    sql[key] = val.replace(val_to_replace, replace_with);})console.log(sql)

JSFiddle: http://jsfiddle.net/d8sZT/