


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!

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools