# JS一维数组转化为三维数组的实现示例

 123456789101112131415161718192021222324 `let arr = [{``     ``'peovince'``: ``'a'``, ``'city'``: ``'b'``, ``'area'``: ``'c'``   ``},``   ``{``     ``'peovince'``: ``'a'``, ``'city'``: ``'b'``, ``'area'``: ``'d'``   ``},``   ``{``     ``'peovince'``: ``'a'``, ``'city'``: ``'e'``, ``'area'``: ``'f'``   ``},``   ``{``     ``'peovince'``: ``'a'``, ``'city'``: ``'e'``, ``'area'``: ``'g'``   ``},``   ``{``     ``'peovince'``: ``'o'``, ``'city'``: ``'p'``, ``'area'``: ``'q'``   ``},``   ``{``     ``'peovince'``: ``'o'``, ``'city'``: ``'p'``, ``'area'``: ``'r'``   ``},``   ``{``     ``'peovince'``: ``'o'``, ``'city'``: ``'s'``, ``'area'``: ``'t'``   ``},``   ``{``     ``'peovince'``: ``'o'``, ``'city'``: ``'s'``, ``'area'``: ``'v'``   ``}];`

 123456789101112131415161718192021222324252627282930313233343536373839404142434445 `let list = Array.from(``new` `Set(``      ``arr.map(item => {``        ``return` `item[``'peovince'``]``      ``})))``    ``let subList = []``    ``list.forEach(res => {``      ``arr.forEach(ele => {``        ``if` `(ele[``'peovince'``] === 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[``'city'``]) !== -1) {``              ``subList[nameArr.indexOf(res)].children[nameArr2.indexOf(ele[``'city'``])].children.push({``                ``value: ele[``'area'``],``                ``label: ele[``'area'``],``              ``})``            ``} ``else` `{``              ``subList[nameArr.indexOf(res)].children.push({``                ``value: ele[``'city'``],``                ``label: ele[``'city'``],``                ``children: [{``                  ``value: ele[``'area'``],``                  ``label: ele[``'area'``],``                ``}]``              ``})``            ``}``          ``} ``else` `{``            ``subList.push({``              ``value: res,``              ``label: res,``              ``children: [{``                ``value: ele[``'city'``],``                ``label: ele[``'city'``],``                ``children: [{``                  ``value: ele[``'area'``],``                  ``label: ele[``'area'``],``                ``}]``              ``}]``            ``})``          ``}``        ``}``      ``})` `    ``})``    ``console.log(subList)`

### ps：JavaScript一维数组转二维数组

 1234567891011 `let array = [1, 2, 3, 4, 5, 6, 7, 8];``len len = array.length;``let n = 4; ``//假设每行显示4个``let lineNum = len % n === 0 ? len / n : Math.floor( (len / n) + 1 );``let res = [];``for` `(let i = 0; i < lineNum; i++) {``  ``// slice() 方法返回一个从开始到结束（不包括结束）选择的数组的一部分浅拷贝到一个新数组对象。且原始数组不会被修改。``  ``let temp = array.slice(i*n, i*n+n);``  ``res.push(temp);``}``console.log(res);`

 12345678910 `let objArray = [{a: 1}, {b: 2}, {c: 3}, {d: 4}, {e: 5}, {f: 6}, {g: 7}];``let len = objArray.length;``let n = 4; ``//假设每行显示4个``let lineNum = len % 4 === 0 ? len / 4 : Math.floor( (len / 4) + 1 );``let res = [];``for` `(let i = 0; i < lineNum; i++) {``  ``let temp = objArray.slice(i*n, i*n+n);``  ``res.push(JSON.parse(JSON.stringify(temp)));``}``console.log(res);`