15
数组对象根据某个值相同合并数组
https://blog.csdn.net/u010214074/article/details/78854778
var arr = [
{"id":"1001","name":"值1","value":"111"},
{"id":"1001","name":"值1","value":"11111"},
{"id":"1002","name":"值2","value":"25462"},
{"id":"1002","name":"值2","value":"23131"},
{"id":"1002","name":"值2","value":"2315432"},
{"id":"1003","name":"值3","value":"333333"}
];
var map = {},
dest = [];
for(var i = 0; i < arr.length; i++){
var ai = arr[i];
if(!map[ai.id]){
dest.push({
id: ai.id,
name: ai.name,
data: [ai]
});
map[ai.id] = ai;
}else{
for(var j = 0; j < dest.length; j++){
var dj = dest[j];
if(dj.id == ai.id){
dj.data.push(ai);
break;
}
}
}
}
console.log(dest);
var obj=[ //https://segmentfault.com/q/1010000010611150
{
date:"2017-08-11",
state:"上",
result:{"温度":4,"湿度":3},
},
{
date:"2017-08-11",
state:"上",
result:{"温度":4,"湿度":3}
},
{
date:"2017-08-11",
state:"下",
result:{"温度":2,"湿度":3}
},
{
date:"2017-08-10",
state:"下",
result:{"温度":5,"湿度":3}
}
];
var temp = [];
obj.forEach(function(item, index) {
var skey = item.date + item.state;
if(typeof temp[skey] == "undefined") {
temp[skey] = item;
} else {
for(var k in item.result) {
temp[skey]["result"][k] += item["result"][k];
}
//temp[skey]["result"]["温度"] += item["result"]["温度"];
//temp[skey]["result"]["湿度"] += item["result"]["湿度"];
}
});
var result = [];
for(var i in temp) {
result.push(temp[i]);
}
console.log(result);
例子:
开发中的例子:
//跟部门同级的成员去重
var topmap = {},topdest = [];
for(let i = 0; i < groupMemberList.memberList.length; i++){
let ai = groupMemberList.memberList[i];
if(!topmap[ai.id]){
topdest.push({
id: ai.id,
realName: ai.realName,
roleContent: [ai.roleContent]
});
topmap[ai.id] = ai;
}else{
for(let j = 0; j < topdest.length; j++){
let dj = topdest[j];
if(dj.id == ai.id){
console.log(ai)
dj.roleContent.push(ai.roleContent);
break;
}
}
}
}
this.memberList = topdest;
console.log(topdest) //去重后的跟部门同级的数据
for(var k = 0;k < groupMemberList.subGroups.length;k++){
//跟组同级的成员去重
var map = {},dest = [];
for(let i = 0; i < groupMemberList.subGroups[k].memberList.length; i++){
let ai = groupMemberList.subGroups[k].memberList[i];
if(!map[ai.id]){
dest.push({
id: ai.id,
realName: ai.realName,
roleContent: [ai.roleContent]
});
map[ai.id] = ai;
}else{
for(let j = 0; j < dest.length; j++){
let dj = dest[j];
if(dj.id == ai.id){
dj.roleContent.push(ai.roleContent);
break;
}
}
}
}
groupMemberList.subGroups[k].memberList = dest;
//组下的成员去重
var subGroupsMap = {},subGroupsDest = [];
for(let i = 0; i < groupMemberList.subGroups[k].subGroups.length; i++){
for(let h = 0; h < groupMemberList.subGroups[k].subGroups[i].memberList.length;h++){
let ai = groupMemberList.subGroups[k].subGroups[i].memberList[h];
if(!subGroupsMap[ai.id]){
subGroupsDest.push({
id: ai.id,
realName: ai.realName,
roleContent: [ai.roleContent]
});
subGroupsMap[ai.id] = ai;
}else{
for(let j = 0; j < subGroupsDest.length; j++){
let dj = subGroupsDest[j];
if(dj.id == ai.id){
dj.roleContent.push(ai.roleContent);
break;
}
}
}
}
groupMemberList.subGroups[k].subGroups[i].memberList = subGroupsDest;
}
console.log(dest)
}
this.lists = groupMemberList; //lists是渲染到页面的去重后的数据
实例二:1.已知数据结构users,请实现语法支持user.unique能够按照name字段去重,并输出结构为:["a","b"]
方法一:没有按照要求
var users=[{
id:1,name:"a"
},{
id:2,name:"a"
},{
id:3,name:"b"
},{
id:4,name:"v"
}]
function unique(users){
var arr = users.map(function(item){
return item.name
})
console.log(arr)
res = Array.from(new Set(arr));
return res;
}
console.log(unique(users)); //["a","b","c"]
方法二:按照要求
var users=[{
id:1,name:"a"
},{
id:2,name:"a"
},{
id:3,name:"b"
},{
id:4,name:"v"
}]
Array.prototype.unique = function () {
var res;
this.map(item => {
this[item.id - 1] = item.name //改为this[index] = item.name,同时上一行代码的(item)改为(item,index)也可以
})
// ES6里新添加了两个很好用的东西,set和Array.from
// set是一种新的数据结构,它可以接收一个数组或者是类数组对象,自动去重其中的重复项目。
res=new Set(this);
console.log("new Set对象",res)
// 但是这里大家可以看到,set返回的是一个对象,但是我们想要的是数组啊。
// 这回,就该轮到Array.from出场了,它的作用,就是可以把类数组对象、可迭代对象转化为数组。
// 这样做也可以
// var arr = users.map(function(item){
// return item.name;
// })
// console.log(arr)
// res=new Set(arr);
// console.log("new Set对象",res)
// // 但是这里大家可以看到,set返回的是一个对象,但是我们想要的是数组啊。
// // 这回,就该轮到Array.from出场了,它的作用,就是可以把类数组对象、可迭代对象转化为数组。
res=Array.from(new Set(this));
return res//es6 数组去重
}
console.log(users.unique());
作者:烟雨空灵
链接:https://juejin.im/post/5be2fcd7f265da616d53aad0
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。