我想使用 JSON.stringify() 在对象中的每个键值对之后添加换行符
在我的实际代码中,我将获得很多键值对,并且新行使其更易于阅读。
示例代码是:
let test = {'key' : ['one', 'two', 'three'], 'keyTwo' : ['one', 'two', 'three']}
let testOne = JSON.stringify(test, null, '\t')
console.log(testOne)
输出:
{
"key": [
"one",
"two",
"three"
],
"keyTwo": [
"one",
"two",
"three"
]
}
我想要:
{
"key": [
"one",
"two",
"three"
],
// <----- newline here
"keyTwo": [
"one",
"two",
"three"
]
}
我已经尝试过
let test = {'key' : ['one', 'two', 'three'] + "\\n", 'keyTwo' : ['one', 'two', 'three']+ "\\n"}
let testOne = JSON.stringify(test, null, '\t\n')
都不起作用
最佳答案
const test = {'key' : ['one', 'two', 'three'], 'keyTwo' : ['one', 'two', 'three'], 'keyThree' : ['one', 'two', 'three']}
const testOne = JSON
.stringify(test, null, "\t")
.replaceAll(
"],\n\t\"",
"],\n\n\t\""
);
console.log(testOne);
关于javascript - 使用 JSON.stringify() 在每个键、值后添加换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71091649/