有如下数据结构。e test1b test2c test2i test3d test3f test3k test3h test3左边是字段1(值),右边是字段2(属性名)把属性名进行去重,然后把左侧的值归并到指定的属性名下,如下格式:[ { category: 'test1', arr: ['e'] }, { category: 'test2', arr: ['b','c'] }, { category: 'test3', arr: ['i','d','k','f','h'] }]我的思路,遍历数组,已经拿到了属性值的数组,形如[ { category: 'test1' }, { category: 'test2' }, { category: 'test3' }].有啥思路方法获取同属性数组然后放到对应的字段名下?
1 回答

拉丁的传说
TA贡献1789条经验 获得超8个赞
这个不难啊,你可以在获取属性数组的时候就新建一个map, 形如:
var cate = new Map()
cate.set('test1', 0)
cate.set('test2', 1)
cate.set('test3', 2)
然后去遍历原数组:
// 属性数组
prop = [
{
category: 'test1',
arr: []
},
{
category: 'test2',
arr: []
},
{
category: 'test3',
arr: []
}
]
// 假设原数组为二维数组
for (i in categorys) {
prop[cate[categorys[i][1]].arr.push(lecategorys[i][0])
}
添加回答
举报
0/150
提交
取消