


Flatten the three-level tree structure data of provinces, cities and districts, and streamline the results according to the selected status
This article introduces how to flatten the three-level tree structure data of provinces, cities and districts, and streamline the results based on the selected status. The original data adopts a tree structure, including province, city, district and county information and selected status ( checked: 1
means selected). The goal is to generate a flat structure that contains only the necessary area code and hierarchy information.
Original data example:
[ { "code": "110000", "value": "Beijing", "checked": "1", "children": [ { "code": "110100", "value": "Beijing", "checked": "1", "children": [ { "code": "110101", "value": "Dongcheng District", "checked": "1" }, { "code": "110102", "value": "Xicheng District", "checked": "1" } ] } ] }, { "code": "120000", "value": "Tianjin City", "checked": "1", "children": [ { "code": "120100", "value": "Tianjin City", "checked": "1", "children": [ { "code": "120101", "value": "Heping District", "checked": "0" }, { "code": "120102", "value": "Hedong District", "checked": "1" } ] } ] } ]
Target flat structure:
If all tertiary regions are selected, only the first-level and second-level area codes are retained; if all tertiary regions are selected, only the first-level area codes are retained; if only the third-level area is selected, only the first-level, second-level and third-level area codes are retained.
[ { "provinceAreald": "110000", "cityAreald": null, "countryAreald": null, "actualAreaLevel": "1" }, { "provinceAreald": "120000", "cityAreald": "120100", "countryAreald": "120102", "actualAreaLevel": "3" } ]
JavaScript function implementation:
The following JavaScript function flattenData
implements data flattening:
function flattenData(data) { const result = []; function processNode(node, parent) { if (node.checked === "1") { const item = { provinceAreald: parent ? parent.code : node.code, cityAreald: null, countryAreald: null, actualAreaLevel: '1' }; if (node.children) { const allChildrenChecked = node.children.every(child => child.checked === "1"); if (allChildrenChecked) { item.actualAreaLevel = node.children[0].children ? '2' : '1'; if(node.children[0].children){ item.cityAreald = node.children[0].code; } } else { item.actualAreaLevel = '2'; item.cityAreald = node.code; for (const child of node.children) { processNode(child, item); } return; } } result.push(item); } else if (node.children) { for (const child of node.children) { processNode(child, node); } } } for (const node of data) { processNode(node); } return result; } // Example usage const data = [/* ...raw data... */]; const flattenedData = flattenData(data); console.log(flattenedData);
This function uses a recursive method to process the tree structure, and dynamically adjust the values of actualAreaLevel
and cityAreald
and countryAreald
fields according to the selected state and the selection of child nodes, and finally generates thin flat data. The code is also optimized, reducing unnecessary function nesting and improving readability and efficiency.
The above is the detailed content of How to flatten the three-level tree structure data of provinces, cities and districts and streamline the results according to the selected status?. For more information, please follow other related articles on the PHP Chinese website!

HTMLtagsdefinethestructureofawebpage,whileattributesaddfunctionalityanddetails.1)Tagslike,,andoutlinethecontent'splacement.2)Attributessuchassrc,class,andstyleenhancetagsbyspecifyingimagesources,styling,andmore,improvingfunctionalityandappearance.

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools
