我有这样一个沉重的阵列:
[ {Id: 1, Name: 'Red', optionName: 'Color'}, {Id: 2, Name: 'Yellow', optionName: 'Color'}, {Id: 3, Name: 'Blue', optionName: 'Color'}, {Id: 4, Name: 'Green', optionName: 'Color'}, {Id: 7, Name: 'Black', optionName: 'Color'}, {Id: 8, Name: 'S', optionName: 'Size'}, {Id: 11, Name: 'M', optionName: 'Size'}, {Id: 12, Name: 'L', optionName: 'Size'}, {Id: 13, Name: 'XL', optionName: 'Size'}, {Id: 14, Name: 'XXL', optionName: 'Size'} ]
我需要做的是将它们分组optionName
并在主数组中有两行,如下所示:
[ { Name: 'Color', Data:[{Id: 1, Name: 'Red'}, {Id: 2, Name: 'Yellow'}, {Id: 3, Name: 'Blue'}, {Id: 4, Name: 'Green'}, {Id: 7, Name: 'Black'}] }, { Name: 'Size', Data:[{Id: 8, Name: 'S'}, {Id: 11, Name: 'M'}, {Id: 12, Name: 'L'}, {Id: 13, Name: 'XL'}, {Id: 14, Name: 'XXL'}] } ]
怎么在javascript中做到这一点?
这是我为这种情况写的片段.您可以将此功能添加到所有阵列:
Object.defineProperty(Array.prototype, 'group', { enumerable: false, value: function (key) { var map = {}; this.forEach(function (e) { var k = key(e); map[k] = map[k] || []; map[k].push(e); }); return Object.keys(map).map(function (k) { return {key: k, data: map[k]}; }); } });
你可以像这样使用它.您只需传递一个定义数据分组方式的函数即可.
var newArray = arr.group(function (item) { return item.optionName; });
工作小提琴
如果你需要,可以替换{key: k, data: map[k]}
使用{Name: k, Data: map[k]}
.
这也是上面代码更紧凑的ES6版本:
Object.defineProperty(Array.prototype, 'group', { enumerable: false, value: function (key) { let map = {}; this.map(e => ({k: key(e), d: e})).forEach(e => { map[e.k] = map[e.k] || []; map[e.k].push(e.d); }); return Object.keys(map).map(k => ({key: k, data: map[k]})); } });
像这样使用它:
let newArray = arr.group(item => item.optionName))