我目前正在处理来自我的数据库的客户退货清单。
客户被保存为从两个连接表构建的对象。 连接表返回重复项,因为在我的First Normal表单数据库中没有类似于数组的数组。
所以考虑像这样的数组
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]);
}
}
答案 0 :(得分:4)
您可以使用一些Array方法来构建新数组,例如Array.prototype.forEach()
forEach()
方法每个数组元素执行一次提供的函数。
some()
方法测试数组中的某个元素是否通过了由提供的函数实现的测试。
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' }
], reduced = [];
customers.forEach(function (a) {
!reduced.some(function (b) {
if (a.id === b.id) {
b.interests.push(a.interest);
return true;
}
}) && reduced.push({ id: a.id, firstname: a.firstname, lastname: a.lastname, interests: [a.interest] });
});
document.write('<pre>' + JSON.stringify(reduced, 0, 4) + '</pre>');
答案 1 :(得分:2)
对customers对象使用array.reduce方法来消除重复。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
请参考以下代码
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'}];
var temp = customers.reduce(function(o,n){
o = o || {};
if(!o[n.firstname+n.lastname+n.id]){
o[n.firstname+n.lastname+n.id] = n;
o[n.firstname+n.lastname+n.id].interest = []
}
o[n.firstname+n.lastname+n.id].interest.push(n.interest);
return o;
},{});
customers = Object.keys(temp).map(function(k) { return temp[k] })
console.log(customers);
&#13;
答案 2 :(得分:1)
使用对象(具有唯一键)来消除重复:
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;
}
}
答案 3 :(得分: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'}];
for (var i=0;i<customers.length;i++){
var intrest=[customers[i].interest];
for (var j=i+1; j<customers.length; j++)
{
if (customers[i].id == customers[j].id){
intrest.push(customers[j].interest);
customers.splice(j,1);
j--;
}
}
customers[i].interest=intrest;
}
console.log(JSON.stringify(customers));