通过js把一个数组修改成多层嵌套多个数组的几种方法总结
作者:前端代码の搬运工
这篇文章主要介绍了通过js把一个数组修改成多层嵌套多个数组的几种方法总结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
通过js把一个数组修改成多层嵌套多个数组
首先一级与二级相同的嵌套
let arr = [ { date: '2020-01-06', age: '18'}, { date: '2020-01-06', age: '25'}, { date: '2020-01-07', age: '34'}, { date: '2020-01-07', age: '18'}, { date: '2020-01-07', age: '38'}, { date: '2020-01-08', age: '26'}, { date: '2020-01-09', age: '24'} ]
let dataArr = []; arr.map(mapItem => { if (dataArr.length == 0) { dataArr.push({ date: mapItem.date, List: [mapItem] }) } else { let res = dataArr.some(item=> {//判断相同日期,有就添加到当前项 if (item.date == mapItem.date) { item.List.push(mapItem) return true } }) if (!res) {//如果没找相同日期添加一个新对象 dataArr.push({ date: mapItem.date, List: [mapItem] }) } } })
结果
dataArr = [ { date:'2020-01-06', List: [ { date: '2020-01-06', age: '18'}, { date: '2020-01-06', age: '25'} ] }, { date:'2020-01-07', List: [ { date: '2020-01-07', age: '34'}, { date: '2020-01-07', age: '18'}, { date: '2020-01-07', age: '38'}, ] }, { date:'2020-01-08', List: [ { date: '2020-01-08', age: '26'}, ] }, { date:'2020-01-09', List: [ { date: '2020-01-09', age: '24' } ] } ]
第二种情况
一级四位数(0001),二级八位数字(00010001)获取的条件是通过一级的四位数字匹配二级的前四位数字
方法1:
var userArr = [ { id: "0001", userName: "laozhang_1" }, { id: "00010001", userName: "laozhang1" }, { id: "00010002", userName: "laozhang1" }, { id: "00010003", userName: "laozhang1" }, { id: "0002", userName: "laowang" }, { id: "00020002", userName: "laozhang2" }, { id: "00020003", userName: "laowang" }, { id: "00020004", userName: "laowang" }, { id: "00020005", userName: "laowang" }, { id: "00020006", userName: "laowang" } ];
let newArr = [] userArr.forEach(item => { if (item.id.length === 4) { newArr.push({ ...item, list: [] }); } }); newArr.forEach(item => { userArr.forEach(user => { if (item.id !== user.id && item.id == user.id.substring(0, 4)) { item.list.push(user); } }); }); console.log(newArr)
方法2:
var userArr = [ { id: "0001", userName: "laozhang_1" }, { id: "00010001", userName: "laozhang1" }, { id: "00010002", userName: "laozhang1" }, { id: "00010003", userName: "laozhang1" }, { id: "0002", userName: "laowang" }, { id: "00020002", userName: "laozhang2" }, { id: "00020003", userName: "laowang" }, { id: "00020004", userName: "laowang" }, { id: "00020005", userName: "laowang" }, { id: "00020006", userName: "laowang" } ];
let newArr = userArr.filter(item => item.id.length === 4) newArr.forEach(item => { item.list = userArr.filter(user => item.id !== user.id && item.id == user.id.substring(0, 4)) }) console.log(newArr)
js一维数组处理成多维数组
let replies = [ { id: 1, content: '1. 真理惟一可靠的标准就是永远自相符合', parent: 0 }, { id: 2, content: '2. 我需要三件东西:爱情友谊和图书', parent: 0 }, { id: 3, content: '2-1 时间是一切财富中最宝贵的财富', parent: 2 }, { id: 4, content: '2-2 我读的书愈多,就愈亲近世界,愈明了生活的意义,愈觉得生活的重要', parent: 2 }, { id: 5, content: '2-1-1 任何事物都是在不断变化的', parent: 3 }, { id: 6, content: '2-1-2 真正的科学家应当是个幻想家', parent: 3 }, { id: 7, content: '3. 难得糊涂,耳不闻人之非,不信谣,不传谣', parent: 0 }, ]; function multidimensionalArray(replies) { let data = JSON.parse(JSON.stringify(replies)); data.forEach(reply => { let parent = data.find(res => res.id === reply.parent); if (parent) { parent.children = parent.children || []; parent.children.push(reply); } }); return data; } console.log(multidimensionalArray(replies)) /**一维数组处理成多维数组 * linearArray 一维数组 *primary 父级id(父子关联字段) * secondary 子级id(父子关联字段) */ multidimensionalArray: function (linearArray, primary, secondary) { let data = JSON.parse(JSON.stringify(linearArray)); data.forEach(item => { let parent = data.find(res => res[primary] === item[secondary]); if (parent) { parent.children = parent.children || []; parent.children.push(item); } }); return data; }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。