我有一个JSON obj,在一些操作(如删除一些作品)后,我打印它,除了我有一些null
值之外,一切看起来都不错。我如何删除这些?从JavaScript对象递归删除空值
我用JSON.stringify(obj, null, 2)
方法来打印,这里是什么样子:
{
"store": {
"book": [
null,
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
null,
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
null,
"price": 19.95
}
}
}
我想这是很多紧凑,很干净(删除多余的3个null
值):
{
"store": {
"book": [
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
你'bicycle'是无效的语法,因为'null'的。 –
我想知道'JSON.stringify'的哪个实现产生了无效的JSON。 –
忽略自行车元素,你就在这里,这是我的错误 – lauxp