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
| var data = [{ "name": "Lete乐特", "avatar": "https://www.lete114.top/img/avatar.png", "link": "https://www.lete114.top", "type": "小白" }, { "name": "Lete乐特个人博客", "avatar": "https://www.lete114.top/img/avatar.png", "link": "https://blog.lete114.top", "type": "大佬" }, { "name": "Lete乐特导航网", "avatar": "https://www.lete114.top/img/avatar.png", "link": "https://webstack.lete114.top", "type": "大佬" }, { "name": "Lete乐特玩音乐", "avatar": "https://www.lete114.top/img/avatar.png", "link": "https://music.lete114.top", "type": "大佬" }]
function groups(data){ let map = {} let result = []
for (let i in data) { let dataIndex = data[i] if (!map[dataIndex.type]) { map[dataIndex.type] = [dataIndex] } else { map[dataIndex.type].push(dataIndex) } }
Object.keys(map).forEach(key => { result.push({ group: key, data: map[key], }) }) return result } console.log(groups(data));
|