简体   繁体   中英

Array Filter in forEach Javascript

I am trying to filter our all JSON objects in that are empty. The below tells me which ones are empty, but I am unable to get the.filter function to return the same data since I have to filter on obj.children and not just obj.

Using the below json_data, it should filter out Newmar. There is another parent level above this, hence the use of the 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": [] } ] }

do you mean

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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