Javascript 对象数组,删除重复项但存储值(即分组)
我目前正在处理数据库中的客户退货列表 客户保存为从两个联接表生成的对象。 由于在我的第一个普通表单数据库中没有类似数组的结构,所以联接表返回重复的数据Javascript 对象数组,删除重复项但存储值(即分组),javascript,arrays,grouping,Javascript,Arrays,Grouping,我目前正在处理数据库中的客户退货列表 客户保存为从两个联接表生成的对象。 由于在我的第一个普通表单数据库中没有类似数组的结构,所以联接表返回重复的数据 考虑这样的数组 var customers = [ {id : 1, firstname : 'John', lastname : 'Doe', interest : 'Cars'}, {id : 1, firstname : 'John', lastname : 'Doe', interest : 'Computers'}, {id : 1,
考虑这样的数组
var customers = [
{id : 1, firstname : 'John', lastname : 'Doe', interest : 'Cars'},
{id : 1, firstname : 'John', lastname : 'Doe', interest : 'Computers'},
{id : 1, firstname : 'John', lastname : 'Doe', interest : 'Babes'},
{id : 2, firstname : 'Frank', lastname : 'Smith', interest : 'Food'},
{id : 2, firstname : 'Frank', lastname : 'Smith', interest : 'Toys'},
{id : 2, firstname : 'Frank', lastname : 'Smith', interest : 'Cake'}];
我一直在拼命尝试,但我只能找到在简单数组中删除重复项的示例,没有对象[1,2,3]
等
最终,我想做的是删除重复项,但要保存兴趣。
这样的对象列表,给定上面的示例数组
var reduced =[
{id : 1, firstname : 'John', lastname : 'Doe' interests : ['Cars', 'Computers', 'Babes']},
{id : 2, firstname : 'Frank', lastname : 'Smith' interests : ['Food', 'Toys', 'Cake']}];
到目前为止,我已尝试删除类似的重复项
for(var i = 0; i < newCustomersArray.length; i++){
console.log(i)
if(i == 0){
console.log('i = 0, customerId ' + newCustomersArray[i].firstname)
data.customers.push(newCustomersArray[i]);
}
//else if(newCustomersArray[i-1].customerId != newCustomersArray[i].customerId){
//
//}
else if( newCustomersArray[i-1].customerId != newCustomersArray[i].customerId){
//console.log(newCustomersArray[i-1].customerId + " == " + newCustomersArray[i].customerId);
console.log('i = '+i+', customerId ' + newCustomersArray[i].firstname)
data.customers.push(newCustomersArray[i]);
}
}
for(var i=0;i
使用对象(具有唯一键)消除重复项:
var tempObj = {};
for(i=0; i<customers.length;i++){
if(tempObj[customers[i].id] == undefined){
tempObj[customers[i].id] = customers[i];
}
else{
tempObj[customers[i].id].interest = tempObj[customers[i].id].interest + ","+customers[i].interest;
}
}
var tempObj={};
对于(i=0;i使用对象(具有唯一键)消除重复项:
var tempObj = {};
for(i=0; i<customers.length;i++){
if(tempObj[customers[i].id] == undefined){
tempObj[customers[i].id] = customers[i];
}
else{
tempObj[customers[i].id].interest = tempObj[customers[i].id].interest + ","+customers[i].interest;
}
}
var tempObj={};
对于(i=0;i您可以使用一些数组方法来构建新数组,如
forEach()
方法对每个数组元素执行一次提供的函数
及
some()
方法测试数组中的某个元素是否通过了所提供函数实现的测试
var客户=[
{id:1,名字:'John',姓氏:'Doe',兴趣:'Cars'},
{id:1,名字:'John',姓氏:'Doe',兴趣:'Computers'},
{id:1,姓:“约翰”,姓“多伊”,兴趣:“宝贝”},
{id:2,姓:“弗兰克”,姓:“史密斯”,兴趣:“食物”},
{id:2,名字:'Frank',姓氏:'Smith',兴趣:'Toys'},
{id:2,姓:“弗兰克”,姓:“史密斯”,兴趣:“蛋糕”}
],减少=[];
客户。forEach(功能(a){
!减少。一些(功能(b){
如果(a.id==b.id){
b、 利益推动(a.利益);
返回true;
}
})&&reduced.push({id:a.id,firstname:a.firstname,lastname:a.lastname,interest:[a.interest]});
});
document.write(“”+JSON.stringify(reduced,0,4)+’;
您可以使用一些数组方法来构建新数组,如
forEach()
方法对每个数组元素执行一次提供的函数
及
some()
方法测试数组中的某个元素是否通过了所提供函数实现的测试
var客户=[
{id:1,名字:'John',姓氏:'Doe',兴趣:'Cars'},
{id:1,名字:'John',姓氏:'Doe',兴趣:'Computers'},
{id:1,姓:“约翰”,姓“多伊”,兴趣:“宝贝”},
{id:2,姓:“弗兰克”,姓:“史密斯”,兴趣:“食物”},
{id:2,名字:'Frank',姓氏:'Smith',兴趣:'Toys'},
{id:2,姓:“弗兰克”,姓:“史密斯”,兴趣:“蛋糕”}
],减少=[];
客户。forEach(功能(a){
!减少。一些(功能(b){
如果(a.id==b.id){
b、 利益推动(a.利益);
返回true;
}
})&&reduced.push({id:a.id,firstname:a.firstname,lastname:a.lastname,interest:[a.interest]});
});
document.write(''+JSON.stringify(reduced,0,4)+'');
在customers对象上使用array.reduce方法来消除重复项。
请参考下面的示例代码
var客户=[
{id:1,名字:'John',姓氏:'Doe',兴趣:'Cars'},
{id:1,名字:'John',姓氏:'Doe',兴趣:'Computers'},
{id:1,姓:“约翰”,姓“多伊”,兴趣:“宝贝”},
{id:2,姓:“弗兰克”,姓:“史密斯”,兴趣:“食物”},
{id:2,名字:'Frank',姓氏:'Smith',兴趣:'Toys'},
{id:2,姓:“弗兰克”,姓:“史密斯”,兴趣:“蛋糕”};
var temp=客户减少(功能(o,n){
o=o |{};
如果(!o[n.firstname+n.lastname+n.id]){
o[n.firstname+n.lastname+n.id]=n;
o[n.firstname+n.lastname+n.id]。兴趣=[]
}
o[n.firstname+n.lastname+n.id].interest.push(n.interest);
返回o;
},{});
customers=Object.keys(temp.map)(函数(k){return temp[k]})
console.log(customers);
在customers对象上使用array.reduce方法以消除重复项。
请参考下面的示例代码
var客户=[
{id:1,名字:'John',姓氏:'Doe',兴趣:'Cars'},
{id:1,名字:'John',姓氏:'Doe',兴趣:'Computers'},
{id:1,姓:“约翰”,姓“多伊”,兴趣:“宝贝”},
{id:2,姓:“弗兰克”,姓:“史密斯”,兴趣:“食物”},
{id:2,名字:'Frank',姓氏:'Smith',兴趣:'Toys'},
{id:2,姓:“弗兰克”,姓:“史密斯”,兴趣:“蛋糕”};
var temp=客户减少(功能(o,n){
o=o |{};
如果(!o[n.firstname+n.lastname+n.id]){
o[n.firstname+n.lastname+n.id]=n;
o[n.firstname+n.lastname+n.id]。兴趣=[]
}
o[n.firstname+n.lastname+n.id].interest.push(n.interest);
返回o;
},{});
customers=Object.keys(temp.map)(函数(k){return temp[k]})
console.log(客户);
您可以编写简单的逻辑
var客户=[
{id:1,名字:'John',姓氏:'Doe',兴趣:'Cars'},
{id:1,名字:'John',姓氏:'Doe',兴趣:'C