簡體   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