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
| var old = [ { id: 1, name: "css", type: "html11", }, { id: 2, name: "css", type: "html22", }, { id: 3, name: "javacript", type: "code33", }, { id: 4, name: "javacript", type: "code44", }, { id: 5, name: "php", type: "drake", }, { id: 6, name: "php", type: "brew", }, { id: 6, name: "php", type: "brewss", }, ] var hash = {} var i = 0 var res = [] old.forEach(function (item) { var name = item.name if (hash[name]) { res[hash[name] - 1].id.push(item.id) res[hash[name] - 1].type.push(item.type) } else { hash[name] = ++i && res.push({ id: [item.id], name: name, type: [item.type], }) } }) console.log(res)
|