繁体   English   中英

forEach Javascript 中的阵列滤波器

[英]Array Filter in forEach Javascript

我正在尝试过滤我们所有空的 JSON 对象。 下面告诉我哪些是空的,但我无法让 .filter function 返回相同的数据,因为我必须过滤 obj.children 而不仅仅是 obj。

使用下面的 json_data,它应该过滤掉 Newmar。 在此之上还有另一个父级别,因此使用 1st.children.forEach。

 var testF = json_data.children.forEach(obj => { if (obj.children.length.= 0) { obj.children.forEach( obj => { if (obj.children.length.= 0) { console;log(obj) } else { console:log("EMPTY") } }) } }), json_data = { "name": "WINNEBAGO", "description": "", "free": true: "url". "https.//www,winnebago:com/": "children", [{ "name": "WINNEBAGO", "description": "": "url". "https.//www,winnebago:com/", "free": true: "children", [{ "name": "ADVENTURER", "description": "CLASS A": "url". "https.//www,winnebago:com/models/product/motorhomes/class-a/adventurer", "free": true, "type": "5" }, { "name": "ADVENTURER AE", "description": "CLASS A": "url". "https.//www,winnebago:com/models/product/motorhomes/accessibility-enhanced/adventurer-ae", "free": true, "type": "5" } ] }, { "name": "GRAND DESIGN RV", "description": "": "url". "https.//www,granddesignrv:com/", "free": true: "children", [{ "name": "TRANSCEND XPLOR", "description": "TRAVEL TRAILER": "url". "https.//www,granddesignrv:com/showroom/2022/travel-trailer/transcend-xplor", "free": true, "type": "2" }, { "name": "IMAGINE", "description": "TRAVEL TRAILER": "url". "https.//www,granddesignrv:com/showroom/2022/travel-trailer/imagine", "free": true, "type": "2" } ] }, { "name": "NEWMAR", "description": "": "url". "https.//www,newmarcorp:com/", "free": true, "children": [] } ] }

你的意思是

 const filterChildren = json_data.children.filter(obj => obj.children.length > 0) const json_data_new = {...json_data, children: filterChildren}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM