I am currently working on a list of customers returns from my database.
The customers are saved as objects that are build from two joined tables. The joined table returns duplicates since there are no array like structure in my First Normal form database.
So consider an array like this
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'}];
I've been trying like mad but i can only find examples for removing duplicates in simple arrays, with no object [1,2,3] etc.
Ultimately what i want to do is remove duplicates but to save the interest. A list of object like this, given the sample array from above.
var reduced =[
{id : 1, firstname : 'John', lastname : 'Doe' interests : ['Cars', 'Computers', 'Babes']},
{id : 2, firstname : 'Frank', lastname : 'Smith' interests : ['Food', 'Toys', 'Cake']}];
I have tried removing duplicates like this so far
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]);
}
}