<script>
let list = [];
let originalList = [{ type: '1', name: "碎花小洋裙", desc: "温柔" }, { type: '1', name: "马丁靴", desc: "泫雅" }, { type: '2', name: "狐狸小皮帽", desc: "温暖" }, { type: '1', name: "碎花小洋裙", desc: "温柔" }, { type: '3', name: "马丁靴", desc: "泫雅" }, { type: '2', name: "狐狸小皮帽", desc: "温暖" }]
let newList = []
//获取所有类型
originalList.forEach(({ type }) => {
list.push(type)
})
//类型去重
list = [...new Set(list)].sort((a, b) => {
return a - b
})
let obj = {}
//改变数据结构填充数组
list.forEach((item) => {
let child = originalList.filter(({ type }) => {
return type == item
})
obj.type = item
obj.child = child
newList.push(obj)
obj = {}
})
console.log(newList)
</script>
单层数组变成嵌套数组操作
最新推荐文章于 2022-03-28 17:30:45 发布