You can use a use reduce to create an object with the values you're looking for:
const test = {a: ' a', b: 'b ', c: ' c '}
const trimAllValues = input => Object.keys(input)
.reduce((prev, next) =>
Object.assign(prev, {
[next]: test[next].replace(/\s+/g, '')
}), {});
const result = trimAllValues(test)
console.log(result)
Lodash's mapValues will be more performant and have better browser support. You can either use lodash, or alternatively have a look at their source and implement the same:
function mapValue(object, iteratee) {
object = Object(object)
const result = {}
Object.keys(object).forEach((key) => {
result[key] = iteratee(object[key], key, object)
})
return result
}
JSON.stringify(obj, (k, v) => typeof v === 'string' ? v.trim() : v)