2016-04-01 12 views
-1

特定の条件チェックに基づいて配列から繰り返しオブジェクトを削除する必要があります この配列の同じ "connector"、 "address"、 "type"値を持つオブジェクトそのオブジェクトを削除します。配列から繰り返し出現を削除する

array = [{ 
     connector : 'smtp', 
     name : 'john', 
     address : '[email protected]', 
     type : 'cc' 
    }, { 
     connector : 'smtp', 
     name : 'john', 
     address : '[email protected]', 
     type : 'cc' 
    }, { 
     connector : 'gtp', 
     name : 'mark', 
     address : '[email protected]', 
     type : 'cc' 
    }, { 
     connector : 'ftp', 
     name : 'wiki', 
     address : '[email protected]', 
     type : 'bcc' 
    }, 
    { 
     connector : 'smtp', 
     name : 'wiki', 
     address : '[email protected]', 
     type : 'cc' 
    } 
] 

私は次のよう

output = [{ 
     connector : 'smtp', 
     name : 'john', 
     address : '[email protected]', 
     type : 'cc' 
    },{ 
     connector : 'gtp', 
     name : 'mark', 
     address : '[email protected]', 
     type : 'cc' 
    }, { 
     connector : 'ftp', 
     name : 'wiki', 
     address : '[email protected]', 
     type : 'bcc' 
    }, 
    { 
     connector : 'smtp', 
     name : 'wiki', 
     address : '[email protected]', 
     type : 'cc' 
    } 
] 
申し訳

のような出力を必要とする私はループし、いくつかの繰り返しのforeachを試みたが、アップなしどこ終わりました。同じことをする効率的な方法を見つけるのを助けてください。

+0

はあなたが試したコードを追加します。 – Tushar

+0

underscore.jsライブラリに探してみてください。彼らは重複フィルタ機能を持っています。 – binariedMe

答えて

2

アンダースコアユニークの助けを借りて行うことができます。

array = [{ 
     connector : 'smtp', 
     name : 'john', 
     address : '[email protected]', 
     type : 'cc' 
    }, { 
     connector : 'smtp', 
     name : 'john', 
     address : '[email protected]', 
     type : 'cc' 
    }, { 
     connector : 'gtp', 
     name : 'mark', 
     address : '[email protected]', 
     type : 'cc' 
    }, { 
     connector : 'ftp', 
     name : 'wiki', 
     address : '[email protected]', 
     type : 'bcc' 
    }, 
    { 
     connector : 'smtp', 
     name : 'wiki', 
     address : '[email protected]', 
     type : 'cc' 
    } 
] 
//generating a string of keys you wish to compare which forms the criteria for uniqueness 
add = _.uniq(array, function(a){ return a.connector + "-" + a.address + "-" + a.type;}) 

console.log(add) 

作業コードhere

+0

「uniqBy」も使用できます。 '_.uniqBy(配列、obj => obj.connector + ' - ' + obj.address + ' - ' + obj.type);' – Tushar

+0

'JSON.stringify'は改善された解決策です、私は –

+0

をお待ちしております。ユニークについては、その文書にはないhttp://underscorejs.org/ – Cyril

関連する問題