Trim white spaces in both Object key and value recursively

后端 未结 8 1980
一生所求
一生所求 2020-12-05 05:53

How do you trim white spaces in both the keys and values in a JavaScript Object recursively?

I came across one issue in which I was trying to \"clean\" a user suppli

8条回答
  •  甜味超标
    2020-12-05 06:05

    You can just stringify it, string replace, and reparse it

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

提交回复
热议问题