1

I want to remove duplicated elements from an array of objects in React JS. My code is as follows :

let cars = [
    {
        id: 1,        
        make: "Volkswagen",
        model: "Golf",
        desc: "2.0 CR TDi Comfortline BMT"
    }, {
        id: 2,
        make: "Audi",
        model: "Q3",
        desc: "2.0 CR TDi Comfortline BMT"        
    }, {
        id: 3,
        make: "Volkswagen",
        model: "Passat CC",
        desc: "2.0 CR TDi Comfortline BMT",

    }, {
        id: 4,
        make: "Volkswagen",
        model: "Golf",
        desc: "1.9 TDI",

    }, {
        id: 5,
        make: "Audi",
        model: "Q5",
        desc: "2.0 CR TDi Comfortline BMT",

    }, {
        id: 6,
        make: "Volkswagen",
        model: "Golf",
        desc: "2.0 CR TDi",

    }
]

class Test extends React.Component {
        getTotalModels(model){
        return cars.filter(c => c.model === model).length;
    }

    getTotalMakes(make){
        return cars.filter(c => c.make === make).length;
    }

        render(){
        return (
        <div>
            {cars.map(c => {            
            return (
                <div key={c.id}>
                <div className="make">{c.make} ({this.getTotalMakes(c.make)})</div>                
                {cars.filter(j => j.make === c.make).map(i => {
                    return <div className="models">{i.model} ({this.getTotalModels(i.model)})</div>
                })}
              </div>
            )
          })}
        </div>
      )
    }
}

React.render(<Test />, document.getElementById('container'));

The result that I get is :

Volkswagen (4)
    Golf (3)
    Passat CC (1)
    Golf (3)
    Golf (3)
Audi (2)
    Q3 (1)
    Q5 (1)
Volkswagen (4)
    Golf (3)
    Passat CC (1)
    Golf (3)
    Golf (3)
Volkswagen (4)
    Golf (3)
    Passat CC (1)
    Golf (3)
    Golf (3)
Audi (2)
    Q3 (1)
    Q5 (1)
Volkswagen (4)
    Golf (3)
    Passat CC (1)
    Golf (3)
    Golf (3)

The result that I want is :

Volkswagen (4)
    Golf (3)
    Passat CC (1)
Audi (2)
    Q3 (1)
    Q5 (1)

I tried with lodash uniq function, but it didn't work.

Here is a fiddle.

2 Answers 2

1

Lodash based solution, using uniq based on car make:

class Test extends React.Component {
        getTotalModels(model){
        return cars.filter(c => c.model === model).length;
    }

    getTotalMakes(make){
        return cars.filter(c => c.make === make).length;
    }

    render(){
        return (
        <div>
            {
          _.uniq(cars, car => car.make)
          .map(c => {            
            return (
                <div key={c.id}>
                <div className="make">{c.make} ({this.getTotalMakes(c.make)})</div>                
                {_.uniq(cars.filter(j => j.make === c.make), car => car.model).map((make, i) => {
                    return <div key={i} className="models">{make.model} ({this.getTotalModels(make.model)})</div>
                })}
              </div>
            )
          })}
        </div>
      )
    }
}

Fiddle

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

1 Comment

This is unique based only on make. I want that it's unique based on make and model.
1

Please check my approach. You can also use library like lodash or underscore to get the unique items from array.

First , get the array of unique elements

function removeDuplicates(originalArray, prop) {
     var newArray = [];
     var lookupObject  = {};

     for(var i in originalArray) {
        lookupObject[originalArray[i][prop]] = originalArray[i];
     }

     for(i in lookupObject) {
         newArray.push(lookupObject[i]);
     }
      return newArray;
 }

var uniqueCars = removeDuplicates(cars, "make");

And , than

return (
        <div>
            {uniqueCars.map(c => {          // Now loop will run for only unique items  
            return (
                <div key={c.id}>
                <div className="make">{c.make} ({this.getTotalMakes(c.make)})</div>                
                {cars.filter(j => j.make === c.make).map(i => {
                    return <div className="models">{i.model} ({this.getTotalModels(i.model)})</div>
                })}
              </div>
            )
          })}
        </div>
      )

http://jsfiddle.net/jwm6k66c/1632/

2 Comments

This is unique based only on make. I want that it's unique based on make and model.
What do mean by this. What is the criteria for unique array? Any way what I understood , I filtered the array having same make. You want to add more criteria, just follow the same approach

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.