1

I'm trying to get an array of unique JSON data based on the comparison of a key value.

In this example, I'm trying to remove any objects with duplicate category values.

Example:

 var products = [
        { category: 'fos', name: 'retek' },
        { category: 'fos', name: 'item' },
        { category: 'nyedva', name: 'blabla' },
        { category: 'fos', name: 'gihi' }
    ];

// array of hold unique values
var uniqueNames = [];

for(i = 0; i< products.length; i++){    
    if(uniqueNames.indexOf(products[i].category) === -1){
        uniqueNames.push(products[i]);        
    }        
}

I'm trying to push to the array any object that doesn't have duplicate category values. Here is a live JSbin.

Please help!

3
  • 1
    Your indexOf checks if uniqueNames already has that category in the array, but as you are pushing the entire object instead of the category, it will never find it and will always push all to the array Commented Aug 24, 2015 at 17:33
  • @Kyle so uniqueNames should only contain {category:'fos',name:'retek'},{ category:'nyedva',name:'blabla' }? Commented Aug 24, 2015 at 17:39
  • @Moogs Yes, it should only contain those two objects Commented Aug 24, 2015 at 17:44

4 Answers 4

1

There are several ways to do this, this is one of them: traverse all the items, and filter out the ones which we have already added with that category. For this we use an object to keep which categories we have seen and which ones are new, so we filter only the seen ones:

var seen = {}

var unique = products.filter(function(item){
    if(seen.hasOwnProperty(item.category)){
        return false;
    }else{
        seen[item.category] = true;
        return true;
    }
})

console.log(unique); // only 2 objects
Sign up to request clarification or add additional context in comments.

Comments

0

When I am trying to do this, I usually put all of the values into a map as keys, since the map data structure will only allow unique keys. So for this case:

var crops = [   { 
    id:     0023,
    crop:   "corn"
},
{
    id:     0034,
    crop:   "corn"
},
{
    id:     0222,
    crop:   "wheat"
}
 ];
var cropsMap = {};
for(var i = 0; i < crops.length; i++) {
   cropsMap[crops[i].crop] = true;
}
var uniqueCrops = Object.keys(cropsMap);

I made a codepen if you want to check it out.

Comments

0
lookup = [];    

for (var product, i = 0; product = products[i++];) {
   var cat = item.category;

   if (!(cat in lookup)) {
     lookup[cat] = 1;
     result.push(products[cat]);
   }
 }

1 Comment

Please consider editing your answer to include an explanation of how your code works.
0

Switch

for(i = 0; i< products.length; i++){    
    if(uniqueNames.indexOf(products[i].category) === -1){
        uniqueNames.push(products[i]);        
    }        
}

To

for(i = 0; i< products.length; i++){    
        if(uniqueNames.indexOf(products[i].category) === -1){
            uniqueNames.push(products[i].category);  // Push Name of category. Will now not place duplicates into UnqiueNames     
        }        
    }

Console

["fos", "nyedva"]

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.