Datamapper and .replace empty cell

Hi guys.
I’m trying to use the .replace javascript function so that when it finds a blank cell within a field, it replaces it with the text of my choice.
For example, I use the following with great success to change text in the field to either differing text or to show as blank, however for the life of me I can’t do it the other way around.

  1. data.extract(‘Field_1’,0).replace(/\OLD_TXT/g, ‘NEW_TEXT’);
  2. data.extract(‘Field_1’,0).replace(/\OLD_TXT/g, ‘’);

Example 1 shows how I replace text from the data file to text that I want in its place;
Example 2 shows how I replace text from the data to show nothing, basically “hiding” it.

I’m thinking (hoping!) there’s a special character for “space” or “blank” or am I barking up the wrong tree? I can get around it by changing the raw data however it would be great if I could work it out on how to do it in datamapper!

Thanks in advance,

Hi Craig,

To check whether a extracted text value is empty you can apply the following JavaScript code:

var field = data.extract("fieldName", 0);

if (field === "") field = "Hello World!";

1 Like

To make it even simpler, you can use the following syntax:

data.extract("fieldName", 0) || "Default value"
1 Like

Thanks Marten - works well :smile:

1 Like

Brilliant Phil - great solution :heart_eyes: