How 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | [{
& #39;品牌': 'xiaomi', '机型': '10', '配置': '512'
},
{
& #39;品牌': 'xiaomi', '机型': '10', '配置': '128'
},
{
& #39;品牌': 'xiaomi', '机型': '11', '配置': '128'
},
{
& #39;品牌': 'xiaomi', '机型': '11', '配置': '64'
},
{
& #39;品牌': 'iPhone', '机型': '10', '配置': '128'
},
{
& #39;品牌': 'iPhone', '机型': '11', '配置': '64'
},
{
& #39;品牌': 'iPhone', '机型': '12', '配置': '64'
},
{
& #39;品牌': 'honor', '机型': '4', '配置': '256'
},
{
& #39;品牌': 'honor', '机型': '5', '配置': '128'
},
{
& #39;品牌': 'honor', '机型': '6', '配置': '128'
}];
|
Copy after login
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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | [
{
"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"
}
]
}
]
}
]
|
Copy after login
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 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
}
|
Copy after login
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!