I'm consuming a JSON file using Axios in my Vue app. One of the fields (country)has a trailing comma and it's causing issues.
JSON
"country": "spain,france,"
....
"country": "spain,belgium,"
...
JS
I tried to replace a word using the code below and this worked fine. It replaced 'france' with 'XXXXXX'
const arr = this.countries;
const newArr = arr.map((countries) => {
if (countries === "france") {
return "XXXXXX";
}
// return countries;
});
console.log("commas " newArr);
I have tried various ways to remove the end comma but I can't seem to work how to. Can anyone help with this, please?
CodePudding user response:
From this point, where you have the "country" value from your XML for e.g. in the variable countryString you could do following:
let countryString = "belgium,france,";
let countries = countryString.split(",").filter(e => e).map((e) => {
if (e === "france") { return "XXXXXX"; }
return e;
});
console.log(countries);
CodePudding user response:
You can use slice to remove last char:
let countryString = "belgium,france,";
countryString = countryString.slice(0, -1);
console.log(countryString)