[
{
"name":"FY20",
"children":[
{
"name":"Quarter1",
"children":[
{
"name":"2018_Oct 17 Manual"
},
{
"name":"2018_Oct 17 Manual"
}
]
},
{
"name":"Quarter2",
"children":[
null,
null,
{
"name":"2018_Oct_fnfjnf 17 Manual"
}
]
},
{
"name":"Quarter3",
"children":[
null,
null,
null,
{
"name":"2018_Octfdmfj 17 Manual"
},
{
"name":"2015ldf_Oct 17 Manual"
},
{
"name":"2018_Oct 17 Manual"
}
]
},
{
"name":"Quarter4",
"children":[
null,
null,
null,
null,
null,
null,
{
"name":"2018_dfdct 17 Manual"
},
{
"name":"2018_fldfslt 17 Manual"
}
]
}
]
},
{
"name":"FY19",
"children":[
{
"name":"Quarter1",
"children":[
null,
null,
null,
null,
null,
null,
null,
null,
{
"name":"2018_Sep 17 Manual"
},
{
"name":"2018_Oct 17 Manual"
},
{
"name":"2018_Nov 17 Manual"
}
]
},
{
"name":"Quarter2",
"children":[
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
{
"name":"2018_Dec 17 Manual"
},
{
"name":"2019_Jan 17 Manual"
},
{
"name":"2019_Feb 17 Manual"
},
{
"name":"2019_Feb 06 Manual"
}
]
},
{
"name":"Quarter3",
"children":[
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
{
"name":"2019_March 17 Manual"
},
{
"name":"2019_April 17 Manual"
},
{
"name":"2019_May 17 Manual"
},
{
"name":"2019_May new_Manual"
}
]
},
{
"name":"Quarter4",
"children":[
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
{
"name":"2019_Jun 17 Manual"
},
{
"name":"2019_Jul 17 Manual"
},
{
"name":"2019_Aug 17 Manual"
}
]
}
]
},
{
"name":"FY18",
"children":[
{
"name":"Quarter4",
"children":[
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
{
"name":"2019_Aug 17 Manual"
},
{
"name":"2019_Aug 17 Manual"
},
{
"name":"2019_Aug 17 Manual"
}
]
}
]
}
]
实际上,我希望上面的JSON对象通过javascript或ES6没有空值
我尝试搜索许多解决方案,但无法为嵌套循环找到正确的解决方案,请提供解决方案或建议以帮助您。
预先感谢
答案 0 :(得分:1)
您可以通过检查值来过滤数据和children
属性。
这种方法会突变给定的数据。
const noNull = v => {
if (v && typeof v === 'object' && Array.isArray(v.children)) v.children = v.children.filter(noNull);
return v !== null;
};
var data = [{ name: "FY20", children: [{ name: "Quarter1", children: [{ name: "2018_Oct 17 Manual" }, { name: "2018_Oct 17 Manual" }] }, { name: "Quarter2", children: [null, null, { name: "2018_Oct_fnfjnf 17 Manual" }] }, { name: "Quarter3", children: [null, null, null, { name: "2018_Octfdmfj 17 Manual" }, { name: "2015ldf_Oct 17 Manual" }, { name: "2018_Oct 17 Manual" }] }, { name: "Quarter4", children: [null, null, null, null, null, null, { name: "2018_dfdct 17 Manual" }, { name: "2018_fldfslt 17 Manual" }] }] }, { name: "FY19", children: [{ name: "Quarter1", children: [null, null, null, null, null, null, null, null, { name: "2018_Sep 17 Manual" }, { name: "2018_Oct 17 Manual" }, { name: "2018_Nov 17 Manual" }] }, { name: "Quarter2", children: [null, null, null, null, null, null, null, null, null, null, null, { name: "2018_Dec 17 Manual" }, { name: "2019_Jan 17 Manual" }, { name: "2019_Feb 17 Manual" }, { name: "2019_Feb 06 Manual" }] }, { name: "Quarter3", children: [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, { name: "2019_March 17 Manual" }, { name: "2019_April 17 Manual" }, { name: "2019_May 17 Manual" }, { name: "2019_May new_Manual" }] }, { name: "Quarter4", children: [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, { name: "2019_Jun 17 Manual" }, { name: "2019_Jul 17 Manual" }, { name: "2019_Aug 17 Manual" }] }] }, { name: "FY18", children: [{ name: "Quarter4", children: [null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, { name: "2019_Aug 17 Manual" }, { name: "2019_Aug 17 Manual" }, { name: "2019_Aug 17 Manual" }] }] }],
result = data.filter(noNull);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 1 :(得分:0)
您可以简单地递归映射值,并在每个级别使用filter(Boolean)
过滤掉错误/空值。
var data = [
{
"name": "FY20",
"children": [
{
"name": "Quarter1",
"children": [
{
"name": "2018_Oct 17 Manual"
},
{
"name": "2018_Oct 17 Manual"
}
]
},
{
"name": "Quarter2",
"children": [
null,
null,
{
"name": "2018_Oct_fnfjnf 17 Manual"
}
]
},
{
"name": "Quarter3",
"children": [
null,
null,
null,
{
"name": "2018_Octfdmfj 17 Manual"
},
{
"name": "2015ldf_Oct 17 Manual"
},
{
"name": "2018_Oct 17 Manual"
}
]
},
{
"name": "Quarter4",
"children": [
null,
null,
null,
null,
null,
null,
{
"name": "2018_dfdct 17 Manual"
},
{
"name": "2018_fldfslt 17 Manual"
}
]
}
]
},
{
"name": "FY19",
"children": [
{
"name": "Quarter1",
"children": [
null,
null,
null,
null,
null,
null,
null,
null,
{
"name": "2018_Sep 17 Manual"
},
{
"name": "2018_Oct 17 Manual"
},
{
"name": "2018_Nov 17 Manual"
}
]
},
{
"name": "Quarter2",
"children": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
{
"name": "2018_Dec 17 Manual"
},
{
"name": "2019_Jan 17 Manual"
},
{
"name": "2019_Feb 17 Manual"
},
{
"name": "2019_Feb 06 Manual"
}
]
},
{
"name": "Quarter3",
"children": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
{
"name": "2019_March 17 Manual"
},
{
"name": "2019_April 17 Manual"
},
{
"name": "2019_May 17 Manual"
},
{
"name": "2019_May new_Manual"
}
]
},
{
"name": "Quarter4",
"children": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
{
"name": "2019_Jun 17 Manual"
},
{
"name": "2019_Jul 17 Manual"
},
{
"name": "2019_Aug 17 Manual"
}
]
}
]
},
{
"name": "FY18",
"children": [
{
"name": "Quarter4",
"children": [
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
{
"name": "2019_Aug 17 Manual"
},
{
"name": "2019_Aug 17 Manual"
},
{
"name": "2019_Aug 17 Manual"
}
]
}
]
}
]
var newData = filterData(data);
function filterData(data) {
return data.map(item => {
if(item && item.children) {
return {
...item,
children: filterData(item.children)
}
}
return item
}).filter(Boolean)
}
console.log(newData);
答案 2 :(得分:0)
null
值似乎只出现在列表中,并且很容易处理,只需将数据转换为字符串,然后将所有出现的null
替换为/不包含,
即可。空字符串。
const data = [{"name":"FY20","children":[{"name":"Quarter1","children":[{"name":"2018_Oct 17 Manual"},{"name":"2018_Oct 17 Manual"}]},{"name":"Quarter2","children":[null,null,{"name":"2018_Oct_fnfjnf 17 Manual"}]},{"name":"Quarter3","children":[null,null,null,{"name":"2018_Octfdmfj 17 Manual"},{"name":"2015ldf_Oct 17 Manual"},{"name":"2018_Oct 17 Manual"}]},{"name":"Quarter4","children":[null,null,null,null,null,null,{"name":"2018_dfdct 17 Manual"},{"name":"2018_fldfslt 17 Manual"}]}]},{"name":"FY19","children":[{"name":"Quarter1","children":[null,null,null,null,null,null,null,null,{"name":"2018_Sep 17 Manual"},{"name":"2018_Oct 17 Manual"},{"name":"2018_Nov 17 Manual"}]},{"name":"Quarter2","children":[null,null,null,null,null,null,null,null,null,null,null,{"name":"2018_Dec 17 Manual"},{"name":"2019_Jan 17 Manual"},{"name":"2019_Feb 17 Manual"},{"name":"2019_Feb 06 Manual"}]},{"name":"Quarter3","children":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,{"name":"2019_March 17 Manual"},{"name":"2019_April 17 Manual"},{"name":"2019_May 17 Manual"},{"name":"2019_May new_Manual"}]},{"name":"Quarter4","children":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,{"name":"2019_Jun 17 Manual"},{"name":"2019_Jul 17 Manual"},{"name":"2019_Aug 17 Manual"}]}]},{"name":"FY18","children":[{"name":"Quarter4","children":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,{"name":"2019_Aug 17 Manual"},{"name":"2019_Aug 17 Manual"},{"name":"2019_Aug 17 Manual"}]}]}];
const res = JSON.stringify(data).replace(/null,?/g, "");
console.log(JSON.parse(res));
答案 3 :(得分:0)
使用递归和过滤器
const filterNulls = values => {
return values.filter(value => {
if (value && value.children) {
value.children = filterNulls(value.children);
}
return value;
});
};