Reputation: 13
i have an array of object menuProduitSet[] , after pushing to it 'menuProduit' which is object of object how can in remove duplicate objects ???
var menuProduitSet = [];
$('select').children('optgroup').children('option:selected').each(function () {
var ch = $(this).parent().parent().parent().parent().attr('class').substring(4, 5);
if (ch !== 'undefined') {
if (ch !== 'c') {
var produit = {"prodId": $(this).val()};
var menuProduit = {menuProduitPK: {menu: menu["menuId"], produit: produit["prodId"], choix: ch}, menu: {menuId: menu["menuId"]}, produit: {prodId: produit["prodId"]}};
menuProduitSet.push(menuProduit);
}
}
});
Upvotes: 0
Views: 110
Reputation: 26
I recommend if you can use Javascript libraries such as underscore or lodash, look at .uniq function. .uniq - Underscore.js - _.uniq - Lodash
So when you have the final array(menuProduitSet):
// by menu.menuId:
var byMenuId = _.uniq(menuProduitSet, function(m){ return m.menu.menuId; });
// by produit.prodId
var byProdId = _.uniq(menuProduitSet, function(m){ return m.produit.prodId; });
Upvotes: 1