1

I am trying to filter out duplicates objects in array by object value name and place into new array called finalResult.

Basically i want to only have objects with unique country names in my new array.

example arrays:

var result = [
   {country: 'united states', numofdistributors: 5},
   {country: 'united states', numofdistributors: 5},
   {country: 'brazil', numofdistributors: 2},
   {country: 'Germany', numofdistributors: 1},
   {country: 'india', numofdistributors: 6},
   {country: 'united states', numofdistributors: 5},
   {country: 'Egypt', numofdistributors: 1},
   {country: 'Germany', numofdistributors: 1},
];

This is what i want my array to turn out like:

var finalResult = [
   {country: 'united states', numofdistributors: 5},
   {country: 'brazil', numofdistributors: 2},
   {country: 'Germany', numofdistributors: 1},
   {country: 'india', numofdistributors: 6},
   {country: 'Egypt', numofdistributors: 1},
];

Here is my attempt to solve with no luck and only frustration:

var finalResults = [];
result.forEach(function(country){
    if(finalResults.indexOf(country.country) != -1){
        console.log('already added');
    }else {
       //console.log('Does not exist');
       finalResults.push(country);
});
console.log(finalResults);
1
  • I made a few changes on your code and updated my answer Commented May 2, 2016 at 20:56

2 Answers 2

2

You could filter the data with Array#filter and use an object for already inserted items.

var result = [{ country: 'united states', numofdistributors: 5 }, { country: 'united states', numofdistributors: 5 }, { country: 'brazil', numofdistributors: 2 }, { country: 'Germany', numofdistributors: 1 }, { country: 'india', numofdistributors: 6 }, { country: 'united states', numofdistributors: 5 }, { country: 'Egypt', numofdistributors: 1 }, { country: 'Germany', numofdistributors: 1 }, ],
    finalResult = result.filter(function (a) {
        var key = a.country + '|' + a.numofdistributors;
        if (!this[key]) {
            this[key] = true;
            return true;
        }
    }, Object.create(null));

document.write('<pre>' + JSON.stringify(finalResult, 0, 4) + '</pre>');

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

2 Comments

Wow, nice idea to provide an empty object as a shared storage.
@Nina Scholz, thank you so much! saved me much headache:)
1

You already done it. Just a simple mistake:

var result = [
   {country: 'united states', numofdistributors: 5},
   {country: 'united states', numofdistributors: 5},
   {country: 'brazil', numofdistributors: 2},
   {country: 'Germany', numofdistributors: 1},
   {country: 'india', numofdistributors: 6},
   {country: 'united states', numofdistributors: 5},
   {country: 'Egypt', numofdistributors: 1},
   {country: 'Germany', numofdistributors: 1},
];

var finalResult = [
   {country: 'united states', numofdistributors: 5},
   {country: 'brazil', numofdistributors: 2},
   {country: 'Germany', numofdistributors: 1},
   {country: 'india', numofdistributors: 6},
   {country: 'Egypt', numofdistributors: 1},
];

var finalResults = [];
var countries = [];
result.forEach(function(country){
    if(countries.indexOf(country.country) != -1){
        console.log('already added');
    }else {
       //console.log('Does not exist');
       finalResults.push(country);
       countries.push(country.country);
}
}
);
console.log(finalResults);

2 Comments

Wow, probably should of considered stepping away from my computer a bit. Thank you for pointing that out.
@TravisMichaelHeller, Yea. Sometimes we need a break and fresh up the brain

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.