0

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);
                        }
                    }
                });
2

1 Answer 1

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

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.