


How to convert one-dimensional array to three-dimensional array in javascript? Method introduction
Dec 30, 2021 am 10:45 AMHow to convert one-dimensional array to three-dimensional array in javascript? This article will share with you a conversion method. This method is enough to convert a JS one-dimensional array into a three-dimensional array!
#Today I saw a friend who asked a question. He wanted to convert a one-dimensional array into a three-dimensional array. He happened to be not very busy and was willing to help. I immediately arranged for this classmate
The following is the one-dimensional array data format returned to us by the back-end classmates
[{ '品牌': 'xiaomi', '机型': '10', '配置': '512' }, { '品牌': 'xiaomi', '机型': '10', '配置': '128' }, { '品牌': 'xiaomi', '机型': '11', '配置': '128' }, { '品牌': 'xiaomi', '机型': '11', '配置': '64' }, { '品牌': 'iPhone', '机型': '10', '配置': '128' }, { '品牌': 'iPhone', '机型': '11', '配置': '64' }, { '品牌': 'iPhone', '机型': '12', '配置': '64' }, { '品牌': 'honor', '机型': '4', '配置': '256' }, { '品牌': 'honor', '机型': '5', '配置': '128' }, { '品牌': 'honor', '机型': '6', '配置': '128' }];
The following is the data format we want to convert (converted into The first level of the three-dimensional array is the brand, the second level is the model, and the third level is the 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, and then pass arr as a parameter to us to convert the array function, the function returns our final three-dimensional array
The following is our arrConversion source code
arrConversion (arr) { let keys = Object.keys(arr[0]) let level1 = keys[0]//获取一级属性名称 let level2 = keys[1]//获取二级属性名称 let level3 = keys[2]//获取三级属性名称 let list = Array.from(new Set( 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(ele[level2]) !== -1) { subList[nameArr.indexOf(res)].children[nameArr2.indexOf(ele[level2])].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
【Related recommendations: javascript learning tutorial】
The above is the detailed content of How to convert one-dimensional array to three-dimensional array in javascript? Method introduction. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

How to remove duplicate elements from PHP array using foreach loop?

PHP array key value flipping: Comparative performance analysis of different methods

PHP array multi-dimensional sorting practice: from simple to complex scenarios

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy

Application of PHP array grouping function in data sorting

Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods

The role of PHP array grouping function in finding duplicate elements

PHP array merging and deduplication algorithm: parallel solution
