0

This array (catalog) contains duplicate values. I need to append duplicate values to an array.

here duplicate entries check by category and by name.

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

$.each(catalog.products, function(index, value) {
  if ($.inArray(value.category, categories) == -1) { 
    //do nothing
  } else {

    if ($.inArray(value.name, categories) == -1) {   
      //do nothing
    } else {
      //add duplicate values to array
      categories.push(value.name);    
    }
  }
});

console.log(categories);

DEMO

12
  • what is your question? Commented Aug 4, 2015 at 10:48
  • @johnigneel how to append duplicate entries to an array Commented Aug 4, 2015 at 10:49
  • by any chance, do you mean remove instead of append ? Commented Aug 4, 2015 at 10:52
  • @atinder yes that's it Commented Aug 4, 2015 at 10:53
  • First thing what i get from your question is you are checking for equality of objects. that check the current object matches with the object present in categories array.... am i right? Commented Aug 4, 2015 at 10:57

3 Answers 3

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

var categories = [],
    duplicates = [];

$.each(catalog.products, function(index, obj) {
    var key = JSON.stringify(obj);
    if (categories[key] === undefined) {
        categories[key] = obj;
    } else {
        duplicates[key] = obj;
    }
});

duplicates = Object.keys(duplicates).map(function (key) {
    return duplicates[key]
});

console.log(categories);
console.log(duplicates);
console.log(duplicates.length);
console.log(duplicates[0]);

demo

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

4 Comments

your answer shows unique entries from array. not duplicate values
console.log(duplicates.length); return zero ?
@PrasanthAR I've just sent you an approach on how to find the duplicates. You can see yourself which type is duplicates and find a way to convert it to an array. I've just edited my answer but I think you're a little bit rude to not investigate yourself further..
i know it's my problem. but i tried to change my attitude.. anyway thanks a lot..
2

Fiddle

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

for (var i=0 ; i< catalog.products.length; i++) {
    var currProduct = catalog.products[i];
    if (uniqueCategories.length){
        for(var j=0; j< uniqueCategories.length; j++) {
           if(currProduct.category === uniqueCategories[j].category && currProduct.name === uniqueCategories[j].name){
            categories.push(currProduct)
           }
       }
   } else {
    uniqueCategories.push(currProduct)
   }
}

console.log(categories);

1 Comment

there is a problem with your answer multiple duplicate values on array, that's never find any way thnx for your answer and help
0

not very sure what you need, but as far as i understand, try the following code.

var duplicates = catalog.products.filter(function(item){
  return catalog.products.some(function(i){
    return i.category == item.category;
  });
})

1 Comment

Like! for using filters. Just started to do the same.

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.