Sta*_*low 39 javascript jquery json
我有一个json数组,看起来像这样:
{
"id": 1,
"children": [
{
"id": 2,
"children": {
"id": 3,
"children": {
"id": 4,
"children": ""
}
}
},
{
"id": 2,
"children": {
"id": 3,
"children": {
"id": 4,
"children": ""
}
}
},
{
"id": 2,
"children": {
"id": 3,
"children": {
"id": 4,
"children": ""
}
}
},
{
"id": 2,
"children": {
"id": 3,
"children": {
"id": 4,
"children": ""
}
}
},
{
"id": 2,
"children": {
"id": 3,
"children": {
"id": 4,
"children": ""
}
}
},
{
"id": 2,
"children": {
"id": 3,
"children": {
"id": 4,
"children": ""
}
}
},
{
"id": 2,
"children": {
"id": 3,
"children": {
"id": 4,
"children": ""
}
}
}]
}
Run Code Online (Sandbox Code Playgroud)
我想有一个函数删除"子"空的元素.我该怎么做?我不是要求答案,只是建议
Lek*_*eyn 57
要遍历对象的键,请使用for .. in循环:
for (var key in json_obj) {
if (json_obj.hasOwnProperty(key)) {
// do something with `key'
}
}
Run Code Online (Sandbox Code Playgroud)
要测试空子项的所有元素,可以使用递归方法:遍历所有元素并递归测试它们的子项.
可以使用以下delete关键字删除对象的属性:
var someObj = {
"one": 123,
"two": 345
};
var key = "one";
delete someObj[key];
console.log(someObj); // prints { "two": 345 }
Run Code Online (Sandbox Code Playgroud)
文档:
| 归档时间: |
|
| 查看次数: |
122628 次 |
| 最近记录: |