I had a string as follows
var data = "10 Watkin Terrace, , , , , Northampton, Northamptonshire";
How to remove the extra ", " in the above data using jquery?
Just pure javascript:
var addresses = data['Addresses'];
for (i in addresses) {
var address = addresses[i];
var parts = address.split(',');
var str = [];
for (j in parts) {
var part = parts[j];
if (part.trim()) {
final.push(part);
}
}
data['Addresses'][i] = str.join(',');
}
console.log(data);
Pure JavaScript:
data = "10 Watkin Terrace, , , , , Northampton, Northamptonshire";
data = data.replace(/(,\s)+/g, '$1');
check this one to Remove unnecessary string
String.prototype.replaceWith = function (str1, str2, ignore) {
return this.replace(new RegEenter code herexp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g, "\\$&"), `enter code here`(ignore ? "gi" : "g")), (typeof(str2) == "string") ? str2.replace(/\$/g, "$$$$") : str2);
};
function simplifyData(dataObject){
for(var key in dataObject){
if(Array.isArray(dataObject[key])){
dataObject[key].forEach(function(val,ind){
if(typeof dataObject[key][ind] == 'string'){
dataObject[key][ind] = dataObject[key][ind].replaceWith(",","");
}
})
}else if(typeof dataObject[key] == 'string'){{
dataObject[key] = dataObject[key].replaceWith(",","");
}
}
}
return dataObject;
}
var data = {
"Latitude": 52.24593734741211,
"Longitude": -0.891636312007904,
"Addresses":
["10 Watkin Terrace, , , , , Northampton, Northamptonshire",
"12 Watkin Terrace, , , , , Northampton, Northamptonshire",
"14 Watkin Terrace, , , , , Northampton, Northamptonshire",
"16 Watkin Terrace, , , , , Northampton, Northamptonshire",
"18 Watkin Terrace, , , , , Northampton, Northamptonshire"]};
var _returnData = simplifyData(data);
console.log(_returnData)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.