Ask Your Question

Revision history [back]

JavaScript replace returns a new string, rather than modifying the string you call it on, so you need to do something like:

records[i].value.name = records[i].value.name.replace(/\\/g,"");

image description

JavaScript replace returns a new string, string rather than modifying the string you call it on, so you need to do something like:

records[i].value.name = records[i].value.name.replace(/\\/g,"");

image description

JavaScript replace returns a new string rather than modifying the string you call it on, so you need to do something like:

records[i].value.name = records[i].value.name.replace(/\\/g,"");

image description

Note that if you are modifying field names rather than their values, you need to build a list of the old keys, a map of the new keys and their values, and then apply the changes. If you try to operate on the keys while looping through them, you'll get a ConcurrentModificationException.

For example, to remove backslashes from field names and values:

for(var i = 0; i < records.length; i++) {
  var record = records[i];
  try {    
    // Build an array of keys to be deleted,
    // and a map of new keys and values
    var oldkeys = [];
    var newvals = {};
    for (key in record.value) {
      newkey = key.replace(/\\/g,"");
      if (newkey != key) {
        oldkeys.push(key);
        newvals[newkey] = record.value[key].replace(/\\/g,"");        
      }
    }

    // Remove the old keys
    for (index in oldkeys) {
      record.value.remove(oldkeys[index]);
    }

    // Add the new ones
    for (key in newvals) {
      record.value[key] = newvals[key];
    }

    // Write record to processor output
    output.write(record);
  } catch (e) {
    // Send record to error
    error.write(record, e);
  }
}

image description