0

I am trying to look through the JSON response and replace every time "Sw" appears in a string(value). For instance, take this JSON object:

[{"accountName":"MTVN\\lalalala",
  "baseOfficeLocation":"657 Hudson Street Floor 11",
  "department":"Mp - Engineering - Engineering",
  "jobTitle":"Sw Test Engineer",
  "preferredName":"Beddingfield,Natasha",
  "workEmail":"Natasha.Beddingfield@viacom.com"}]

I'd like to replace everytime I get a response that includes those butchered words like "Mp" and "Sw"and replace them with it's whole word. So "Sw" would become "Software".

Here is some code i have so far, but I am not that experienced at all in node.js, so would like to know how to accomplish this (:

var baseOfficeLocation = jsonData.baseOfficeLocation;
            console.log("Base Office Location: " + baseOfficeLocation);
            var department = jsonData.department;
            console.log("Department: " + department);

            var workEmail = jsonData.workEmail;
            console.log("Work Email: " + workEmail);

            if (jsonData.jobTitle !== null) {
                replaceString(jsonData.jobTitle, 'Sw', 'Software');
            }
            console.log("Job Title: " + jobTitle);
VeeArr
  • 31
  • 4

2 Answers2

0
var demo = [{"accountName":"MTVN\\lalalala",
  "baseOfficeLocation":"657 Hudson Street Floor 11",
  "department":"Mp - Engineering - Engineering",
  "jobTitle":"Sw Test Engineer",
  "preferredName":"Beddingfield,Natasha",
  "workEmail":"Natasha.Beddingfield@viacom.com"}]


console.log(replaceString(demo[0].jobTitle, 'Sw', 'Software'));  // Software Test Engineer

function replaceString(str, substr, newSubstr){
    var re = new RegExp(substr,"i");
    return str.replace(re, newSubstr);
}
anonystick
  • 362
  • 1
  • 9
  • thanks so much, @NguyenTungs! How would i change several strings at once if they appear in the string response. For example, let's say in one response we have "Sfw" which i would want to replace with software, but in another response, "Sw" appears. So the ability for it to pick up many options from a string a change them with various possible replacement strings of our choosing? – VeeArr May 20 '17 at 06:03
0

Here is the solution to the replacement of string:

var demo = [{"accountName":"MTVN\\lalalala",
    "baseOfficeLocation":"657 Hudson Street Floor 11",
    "department":"Mp - Engineering - Engineering",
    "jobTitle":"Sw Test Engineer",
    "preferredName":"Beddingfield,Natasha",
    "workEmail":"Natasha.Beddingfield@viacom.com"}]
for(var key in demo[0]) {
    demo[0][key] = demo[0][key].replace('Sw', 'Software');
}
mn.agg
  • 281
  • 1
  • 8