2016-04-28 25 views
1

我試圖從具有4個相同值和1個唯一的數組中刪除對象。從數組中刪除類似的對象

甲快速谷歌搜索給出很多選擇用於從陣列中除去相同對象,但不是說是類似對象。陣列的

實施例:

var data = [ 
    {Date: "2016-04-27T09:03:45Z", Name: "Tito", Title: "Developer", Department:"IT", Company: "XXX"}, 
    {Date: "2016-04-27T08:07:45Z", Name: "Tito", Title: "Developer", Department:"IT", Company: "XXX"}, 
    {Date: "2016-04-27T10:23:45Z", Name: "Tito", Title: "Developer", Department:"IT", Company: "XXX"} 
] 

我已經使用lodash _uniq功能嘗試,但只需要一個屬性匹配:

var non_duplidated_data = _.uniq(data, 'Name'); 

所有值均爲除日期相同。我如何根據4個屬性刪除相同的對象?

+0

可能重複[下劃線/ lodash唯一的多個屬性](http://stackoverflow.com/questions/26306415/underscore-lodash-unique-by-multiple-properties) – DhruvPathak

+0

請考慮接受答案 – Manticore

回答

1

你可以使用純Javascript來解決這個問題。 只要檢查每個對象的每個屬性:

Array.prototype.allValuesSame = function() { 
    for(var i = 1; i < this.length; i++) { 
     if(this[i] !== this[0]) 
      return false; 
    } 
    return true; 
} 

var data = [ 
    {Date: "2016-04-27T09:03:45Z", Name: "Tito", Title: "Developer", Department:"IT", Company: "XXX"}, 
    {Date: "2016-04-27T08:07:45Z", Name: "Tito", Title: "Developer", Department:"IT", Company: "XXX"}, 
    {Date: "2016-04-27T10:23:45Z", Name: "Tito", Title: "Developer", Department:"IT", Company: "XXX"} 
] 

var tmpPropValues = {}; 
for (var property in data[0]) { 
    tmpPropValues[property] = []; 

    for(var i = 0; i < data.length; i++) { 
     var obj = data[i]; 

    // store temporary in array 
    if (obj.hasOwnProperty(property)) { 
     var value = obj[property]; 

     tmpPropValues[property].push(value); 
    } 
    } 

    if(tmpPropValues[property].allValuesSame()) { 
     delete tmpPropValues[property]; 
    } 
} 

for(var prop in tmpPropValues) { 
    var tmp = document.getElementById("result").innerHTML; 
    document.getElementById("result").innerHTML = tmp+" "+prop; 

} 

我爲你做了一個fiddle

1

使用.uniqBy https://lodash.com/docs#uniqBy,它允許自定義標準來統一你的數組。

+0

我是理解語法有點麻煩。我試過這個'_.uniqBy($ scope.data,「Job_Title」,「Department」);'它只根據第一個參數'Job_Title'進行計算。有什麼建議麼? – TietjeDK

+0

@TietjeDK使用這裏給出的好方法:http://stackoverflow.com/questions/26306415/underscore-lodash-unique-by-multiple-properties – DhruvPathak

1

你可以通過兩個嵌套循環來完成這個循環,它將自己迭代數組。

var newArray = []; //Result array 

//iterate 
data.forEach(function(item, index){ 

    var found = false; 

    //Go through the rest of elements and see if any matches 
    for (var i = index; i < data.length, i++) { 
     if (item.name == data[i].name && item.title == data[i].title) // add more fields here 
      found = true;  
    } 

    //Add to the result array if no duplicates found 
    if (!found) 
     newArray.push(item); 
}) 
1

這可以幫助你,在這段代碼我通過映射應該比較所有屬性開始,然後我降低,從而只拿到了獨特的記錄數組。

var data = [ 
     {Date: "2016-04-27T09:03:45Z", Name: "Tito", Title: "Developer", Department:"IT", Company: "XXX"}, 
     {Date: "2016-04-27T08:07:45Z", Name: "Tito", Title: "Developer", Department:"IT", Company: "XXX"}, 
     {Date: "2016-04-27T10:23:45Z", Name: "Tito", Title: "Developer", Department:"IT", Company: "XXX"} 
    ]; 

var uniqueRecord = data.map(function(item){ 
    return {Name:item.Name,Title:item.Title,Department: item.Department,Company:item.Company}; 
}).reduce(function(acc,curr){ 
    if(!Array.isArray(acc)){ 
     var copyOfAcc = JSON.parse(JSON.stringify(acc)); 
     acc = []; 
     acc.push(copyOfAcc); 
    } 
    return (acc.indexOf(curr) > -1) ? acc.push(curr) : acc; 

}); 

其結果將是:

[ { Name: 'Tito', 
    Title: 'Developer', 
    Department: 'IT', 
    Company: 'XXX' } ] 

然後,你需要定義你想保留該日(第一,最後,這?),並再次找到了記錄,對於這個簡單的「 forEach'應該工作。