2017-01-31 7 views
0

オブジェクトの配列であるmenuProduitSet []、 を押した後に 'menuProduit'オブジェクトのオブジェクトです 重複するオブジェクトをどのように削除できますか?javacriptを使用して配列から重複するオブジェクトを削除します

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); 
         } 
        } 
       }); 
+0

http://stackoverflow.com/questions/2218999/remove-duplicates-from-an-array-of-私が1つのオブジェクトに3つのオブジェクトを持っているので、オブジェクト内のJavaScriptは –

+1

では機能しません –

答えて

1

私はあなたがそのようなアンダースコアやlodashなどのJavascriptライブラリを使用することができれば、.uniq機能を見てお勧めします。 .uniq - Underscore.js - _.uniq - Lodash

だから、最終的な配列(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; }); 
関連する問題