mardi 4 août 2015

how to append duplicate values from json array to another array? not remove from array

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

Aucun commentaire:

Enregistrer un commentaire