1

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]);
        }
    }
10
  • 2
    Why can't you try group by in database query so that duplicates can avoid there itself? Commented Nov 25, 2015 at 9:59
  • Yep, a simple grouping can solve your problem on server side. Doing it yourself, especially in javascript, will be clumsy and expansive Commented Nov 25, 2015 at 10:00
  • Sorry, should have added some context, I have tried removing duplicates so far, ill add the code bit in the question. Saving the interests in the list however is a problem i cant wrap my head around so far, Commented Nov 25, 2015 at 10:01
  • Will it always be in order by id? Commented Nov 25, 2015 at 10:04
  • 1
    Do it on the server side. GROUP BY and GROUP_CONCAT in MySql will be a good starting point. Commented Nov 25, 2015 at 10:10

4 Answers 4

4

You can use some of the Array methods to build your new array, like Array.prototype.forEach()

The forEach() method executes a provided function once per array element.

and Array.prototype.some()

The some() method tests whether some element in the array passes the test implemented by the provided function.

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>');

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, great solution, and great with some explenation. Works like a charm
2

Use array.reduce method on customers object to eliminate duplicates. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

Please refer sample below code

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);

Comments

1

Use an objects (that have unique keys) to eliminate duplicates:

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;
    }
}

here a demo https://jsfiddle.net/h2aeeth6/

Comments

1

You can write just simple logic,

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.