[Vuejs]-Is there a way to ignore whitespace inside of the value of a <option> tag?

0👍

You can trim both key and value using the following function:

function trimObj(obj) {
  if (!Array.isArray(obj) && typeof obj != 'object') return obj;
  return Object.keys(obj).reduce(function(acc, key) {
    acc[key.trim()] = typeof obj[key] == 'string'? obj[key].trim() : trimObj(obj[key]);
    return acc;
  }, Array.isArray(obj)? []:{});
}

var a = '{ "name":"John ", "age":30, "city":" New York"}';
console.log(trimObj(JSON.parse(a)));

Or alternatively, You can stringify it, string replace, and reparse it

JSON.parse(JSON.stringify(badJson).replace(/"\s+|\s+"/g,'"'))

Answered here

Leave a comment