现有原数据
let arr = [
{a:'age',b:18,c:1,...},
{a:'age',b:19,c:1,...},
{a:'age',b:18,c:2,...,
{a:'sex',b:'m',.....}
....]
数据处理方法:
function arrObjToObjArr(arrObj,objKey,objValue){
let Obj={}
for(let i=0;i<arrObj.length;i++){
for(let key in arrObj[i]){
if(!(key in Obj)){
Obj[objKey] = []
}
Obj[objKey].push(arrObj[i][objValue])
}
}
return Obj
}
function repeatObjValueByKey(obj){//对象指定keys数组去重复
for(let key in obj){
obj[key] = _.uniqWith(Obj[key],_.Equal)//_为lodash引入别名
}
return obj
}
使用arrObjToObjArr
let obj = arrObjToObjArr(arr,'a','b')
获得obj
{
age:[18,19,18],
sex:['m']
}
使用repeatObjValueByKey
obj = repeatObjValueByKey(obj)
获得去重后的obj
{
age:[18,19],
sex:['m']
}
网友评论