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);
}
}
});
-
stackoverflow.com/questions/2218999/…Lavekush– Lavekush2017年01月31日 11:59:42 +00:00Commented Jan 31, 2017 at 11:59
-
1not working because i have 3 objects in one objectZaki Meddeb– Zaki Meddeb2017年01月31日 12:03:22 +00:00Commented Jan 31, 2017 at 12:03
1 Answer 1
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; });
Sign up to request clarification or add additional context in comments.
Comments
lang-js