Copy link to clipboard
Copied
Ok so I am concatentating strings together for a full address in two fields. First field is fine, it uses all the fields and works fine. However, in the second address field it will say "SAME AS BLOCK 3" by default but it has my extra commas from my script in it. I am trying s.replace but I don't know what I'm doing and I can't really find anything that solves my problem. I'm not even sure if that is the right code I should be using.
Can someone walk me through my code so I can learn?
var s = this.getField("ADDRESS.1").valueAsString + ", "
+ this.getField("CITY.1").valueAsString + ", "
+ this.getField("COUNTY.1").valueAsString + ", "
+ this.getField("STATE.1").valueAsString + ", "
+ this.getField("COUNTRY.1").valueAsString + ", "
+ this.getField("ZIP.1").valueAsString
event.value = s.replace(/^, |, $/g, "", ', , ');
OUTPUT:
SAME AS BLOCK 3, , , ,
What I'm looking for:
SAME AS BLOCK 3
Copy link to clipboard
Copied
To be honest, this is a pretty bad way of doing it. I would do it like this:
var fields = ["ADDRESS.1", "CITY.1", "COUNTY.1", "STATE.1", "COUNTRY.1", "ZIP.1"];
var values = [];
for (var i in fields) {
var f = this.getField(fields[i]);
var v = f.valueAsString;
if (v!="") values.push(v);
}
event.value = values.join(", ");
Copy link to clipboard
Copied
Drop the markers for the first and last characters in your RegExp (^ and $, resp.), and it will work.
Copy link to clipboard
Copied
Also, the replace function only takes two input parameters. What's the third one for?
Copy link to clipboard
Copied
Like this?
Old:
event.value = s.replace(/^, |, $/g, "", ', , ');
New:
event.value = s.replace(/, |,/g, "", ', , ');
Also, the replace function only takes two input parameters. What's the third one for?
By @try67
I have no idea even what the 1st and 2nd do. To me it looks like 5 different parameters. I'm used to programming excel. I found it and was just trying to figure it out. I'm not even sure whats going on in that. I've been trying to find an answer on parameters since last night, I just don't know enough to be able to convey what I'm looking for.
Copy link to clipboard
Copied
To be honest, this is a pretty bad way of doing it. I would do it like this:
var fields = ["ADDRESS.1", "CITY.1", "COUNTY.1", "STATE.1", "COUNTRY.1", "ZIP.1"];
var values = [];
for (var i in fields) {
var f = this.getField(fields[i]);
var v = f.valueAsString;
if (v!="") values.push(v);
}
event.value = values.join(", ");
Copy link to clipboard
Copied
To be honest, this is a pretty bad way of doing it. I would do it like this:
var fields = ["ADDRESS.1", "CITY.1", "COUNTY.1", "STATE.1", "COUNTRY.1", "ZIP.1"];
var values = [];
for (var i in fields) {
var f = this.getField(fields[i]);
var v = f.valueAsString;
if (v!="") values.push(v);
}
event.value = values.join(", ");
By @try67
Ok so this retrieves the values of the fields, puts it in order, places the comma, but if blank it pushes nothing into the string and leaves no joining comma. I had to look up values.push to gleen info from that.
Copy link to clipboard
Copied
I can confirm that you have correctly described exactly how the script works. A value is pushed onto the "values" array only if it is non-blank.
Is this the result you wanted? Was there another question?
Copy link to clipboard
Copied
No thats it. I'm trying to learn.