How to convert one-dimensional array to three-dimensional array in javascript
This article mainly shows you "how to convert an one-dimensional array into a three-dimensional array in javascript", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and learn "how to convert an one-dimensional array into a three-dimensional array in javascript".
The following is the one-dimensional array data format returned to us by the back-end students
[{'brand': 'xiaomi',' model':'10', 'configuration': '512'}, {'brand': 'xiaomi',' model':'10', 'configuration': '128'}, {'brand': 'xiaomi',' model':'11', 'configuration': '128'} {'Brand': 'xiaomi',' model':'11', 'configuration':'64'}, {'brand': 'iPhone',' model':'10', 'configuration': '128'}, {'brand': 'iPhone',' model':'11', 'configuration':'64'} {'brand': 'iPhone',' model':'12', 'configuration':'64'}, {'brand': 'honor',' model':'4', 'configuration': '256'}, {'brand': 'honor',' model':'5', 'configuration': '128'} {'Brand': 'honor',' model':'6', 'configuration': '128'}]
The following is the data format we want to convert (into a three-dimensional array, the first level is brand, the second level is model, and the third level is configuration)
[{"value": "xiaomi", "label": "xiaomi", "children": [{"value": "10", "label": "10", "children": [{"value": "512" "label": "512"}, {"value": "128"," label ":" 128}]}, {"value": "11", "label": "11" "children": [{"value": "128"," label ":" 128"}, {"value": "64" "label": "64"}]}}, {"value": "iPhone", "label": "iPhone", "children": [{"value": "10", "label": "10" "children": [{"value": "128"," label ":" 128}]}, {"value": "11", "label": "11" "children": [{"value": "64", "label": "64"}]}, {"value": "12", "label": "12" "children": [{"value": "64", "label": "64"}]}}, {"value": "honor", "label": "honor" "children": [{"value": "4", "label": "4", "children": [{"value": "256"," label ":" 256"}]} {"value": "5", "label": "5", "children": [{"value": "128"," label ":" 128}]}, {"value": "6" "label": "6", "children": [{"value": "128"," label ":" 128}]}]]
First we define an arr variable to receive our one-dimensional array, then pass arr as an argument to our function that converts the array, and the function returns our final three-dimensional array.
Here is our arrConversion source code
ArrConversion (arr) {let keys = Object.keys (arr [0]) let level1 = keys [0] / / get the primary attribute name let level2 = keys [1] / / get the secondary attribute name let level3 = keys [2] / / get the tertiary attribute name let list = Array.from (arr.map (item = > {return item [level1]}) ) let subList = [] list.forEach (res = > {arr.forEach (ele = > {if (ele [level1] = res) {let nameArr = subList.map (item = > item.value) if (nameArr.indexOf (res)! =-1) {let nameArr2 = sublist [nameArr.indexOf (res)] .children.map (item = > item.value) If (nameArr2.indexOf (elelevel2])! =-1) {sublist [nameArr.indexOf (res)] .children [nameArr2.indexOf (elelevel2]) .children.push ({value: ele [level3]) Label: ele [level3],})} else {sublist [nameArr.indexOf (res)] .children.push ({value: ele [level2], label: ele [level2], children: [{value: ele [level3]) Label: ele [level3],}]}} else {subList.push ({value: res, label: res, children: [{value: ele [level2]) Label: ele [level2], children: [{value: ele [level3], label: ele [level3],}]}]}) return subList}
The output result is correct
These are all the contents of the article "how to convert an one-dimensional array into a three-dimensional array in javascript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!