You can use the String.replace()
function
This function is a member of the String prototype. It is not available for the array prototype because not all arrays contain strings.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
Assuming the values that result from this line:
var roleSummary = data.map(function(r){ return r[1]});
Would be something like:
var roleSummary = [
"Summary 1",
"Summary 2",
"Summary 3",
"Summary 4",
"Summary 5",
"Summary 6"
]
You could process them with another map
function like what you have used:
var roleSummaryReplaced = roleSummary.map(function(val){
return val.replace("Summary", "Smry");
})
Which would result in all values that matched "Summary" to be replaced with "Smry":
[
"Smry 1",
"Smry 2",
"Smry 3",
"Smry 4",
"Smry 5",
"Smry 6"
]
You could do it all within the original line too:
// Instead of:
var roleSummary = data.map(function(r){ return r[1]});
// Do this:
var roleSummary = data.map(function(r){
return r[1].replace("text to be replaced", "text to replace with");
});
Extra note:
If the individual string values in the array may contain more than one occurrence of the text to be replaced
, then modify your replace function to use regex and include the global flag:
var regex = /text to be replaced/g;
var roleSummary = data.map(function(r){
return r[1].replace(regex, "text to replace with");
});
Or all on the same line:
var roleSummary = data.map(function(r){
return r[1].replace(/text to be replaced/g, "text to replace with");
});
Reference
String.prototype.replace
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…