2016-11-08 64 views
2

我有以下對象數組。從對象數組中刪除元素javascript

[{"rId":24,"gId":40,"sId":20,"disabled":false}, 
{"rId":24,"gId":40,"sId":19,"disabled":false}, 
{"rId":24,"gId":40,"sId":50,"disabled":false}, 
{"rId":24,"gId":40,"sId":20,"disabled":true}, 
{"rId":24,"gId":40,"sId":19,"disabled":true}, 
{"rId":24,"gId":40,"sId":50,"disabled":true}, 
{"rId":24,"gId":39,"sId":18,"disabled":false}] 

其中有些記錄是對立的ex。 1st元素和4th元素具有相同的rId,gId和sId,但禁用標誌是相反的。 我想消除所有這些記錄。

我的預期陣列是{"rId":24,"gId":39,"sId":18,"disabled":false}(消除所有的對立面記錄)

我嘗試下面的代碼,但它給我錯誤的輸出。

arrOfObj=[{"rId":24,"gId":40,"sId":20,"disabled":false}, 
{"rId":24,"gId":40,"sId":19,"disabled":false}, 
{"rId":24,"gId":40,"sId":50,"disabled":false}, 
{"rId":24,"gId":40,"sId":20,"disabled":true}, 
{"rId":24,"gId":40,"sId":19,"disabled":true}, 
{"rId":24,"gId":40,"sId":50,"disabled":true}, 
{"rId":24,"gId":39,"sId":18,"disabled":false}] 


$.each(arrOfObj,function (index1,firstObj) { 
    $.each(arrOfObj,function (index2,secondObj) { 
     if(index1>= index2){ 
      return true; 
     } 
     var areObjAntithesis=firstObj.rId===secondObj.rId && firstObj.gId===secondObj.gId 
      && firstObj.sId===secondObj.sId && firstObj.disabled!==secondObj.disabled; 

     if(areObjAntithesis){ 
      arrOfObj.splice(index1,1); 
      arrOfObj.splice(index2,1) 
      return false; 
     } 
    }) 
}) 

是否有任何優雅的方式來實現預期的輸出?

回答

1

您可以用map()做到這一點,filter()

var data = [{"rId":24,"gId":40,"sId":20,"disabled":false}, 
 
{"rId":24,"gId":40,"sId":19,"disabled":false}, 
 
{"rId":24,"gId":40,"sId":50,"disabled":false}, 
 
{"rId":24,"gId":40,"sId":20,"disabled":true}, 
 
{"rId":24,"gId":40,"sId":19,"disabled":true}, 
 
{"rId":24,"gId":40,"sId":50,"disabled":true}, 
 
{"rId":24,"gId":39,"sId":18,"disabled":false}] 
 
    
 
var ar = data.map(function(e) { 
 
    return e.rId + '|' + e.gId + '|' + e.sId; 
 
}); 
 
    
 
var result = data.filter(function(e) { 
 
    var key = e.rId + '|' + e.gId + '|' + e.sId; 
 
    return ar.indexOf(key) == ar.lastIndexOf(key); 
 
}); 
 

 
console.log(result)

0

您可以使用多個array.filter和核查計,只有迴歸具有超過1個值的元素和如果值是相同的或僅具有一個值

var data = [{"rId":24,"gId":40,"sId":20,"disabled":false}, 
 
{"rId":24,"gId":40,"sId":19,"disabled":false}, 
 
{"rId":24,"gId":40,"sId":50,"disabled":false}, 
 
{"rId":24,"gId":40,"sId":20,"disabled":true}, 
 
{"rId":24,"gId":40,"sId":19,"disabled":true}, 
 
{"rId":24,"gId":40,"sId":50,"disabled":true}, 
 
{"rId":24,"gId":39,"sId":18,"disabled":false}] 
 

 
var result = data.filter(function(outer){ 
 
    var disablesValues = [] 
 
    
 
    var _r = data.filter(function(inner){ 
 
    if(inner.gId === outer.gId && inner.sId === outer.sId){ 
 
     if(disablesValues.indexOf(inner.disabled) < 0) 
 
     disablesValues.push(inner.disabled); 
 
     return true; 
 
    } 
 
    }); 
 
    
 
    return _r.length === 1 || disablesValues.length === 1 
 
}); 
 

 
console.log(result)

0

你可以兩個迴路,一個用於收集和一個用於過濾所述陣列。

var data = [{ "rId": 24, "gId": 40, "sId": 20, "disabled": false }, { "rId": 24, "gId": 40, "sId": 19, "disabled": false }, { "rId": 24, "gId": 40, "sId": 50, "disabled": false }, { "rId": 24, "gId": 40, "sId": 20, "disabled": true }, { "rId": 24, "gId": 40, "sId": 19, "disabled": true }, { "rId": 24, "gId": 40, "sId": 50, "disabled": true }, { "rId": 24, "gId": 39, "sId": 18, "disabled": false }], 
 
    hash = Object.create(null), 
 
    getKey = function (o) { return ["rId", "gId", "sId"].map(function (k) { return o[k]; }).join('|'); }, 
 
    result; 
 

 
data.forEach(function (a) { 
 
    var key = getKey(a); 
 
    hash[key] = (hash[key] || 0) + (a.disabled || -1); 
 
}); 
 

 
result = data.filter(function (a) { 
 
    return hash[getKey(a)]; 
 
}); 
 

 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

ES6與Array#find

var data = [{ "rId": 24, "gId": 40, "sId": 20, "disabled": false }, { "rId": 24, "gId": 40, "sId": 19, "disabled": false }, { "rId": 24, "gId": 40, "sId": 50, "disabled": false }, { "rId": 24, "gId": 40, "sId": 20, "disabled": true }, { "rId": 24, "gId": 40, "sId": 19, "disabled": true }, { "rId": 24, "gId": 40, "sId": 50, "disabled": true }, { "rId": 24, "gId": 39, "sId": 18, "disabled": false }], 
 
    result = data.filter(a => 
 
     !data.find(b => ["rId", "gId", "sId"].every(k => 
 
      a[k] === b[k] 
 
     ) && a.disabled !== b.disabled)); 
 

 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

0

這裏是函數式編程風格ES6解決方案,這將涉及更多的重複,以及,計數浩瓦特的禁用的數量和啓用對象平衡彼此湮滅:

function eliminateOpposites(arr) { 
 
    return [...arr 
 
     .map(o => ({ o, k: JSON.stringify({ rId:o.rId, gId:o.gId, sId:o.sId }) })) 
 
     .reduce((acc, o) => acc.set(o.k, (acc.get(o.k) || 0)+ (+o.o.disabled || -1)), 
 
          new Map())] 
 
     .filter(([k, balance]) => balance) 
 
     .map(([k, balance]) => Object.assign(JSON.parse(k), {disabled: balance>0})); 
 
} 
 

 
// Sample data 
 
var arrOfObj=[ 
 
{"rId":24,"gId":40,"sId":20,"disabled":false}, 
 
{"rId":24,"gId":40,"sId":19,"disabled":false}, 
 
{"rId":24,"gId":40,"sId":50,"disabled":false}, 
 
{"rId":24,"gId":40,"sId":20,"disabled":true}, 
 
{"rId":24,"gId":40,"sId":19,"disabled":true}, 
 
{"rId":24,"gId":40,"sId":50,"disabled":true}, 
 
{"rId":24,"gId":39,"sId":18,"disabled":false}] 
 
    
 
console.log(eliminateOpposites(arrOfObj));

它利用散列的,這導致了爲O(n)算法代替O(N²),這是indexOf風格的解決方案。

JSON.stringifyJSON.parse用於組合和分解組合鍵值。字符串版本在Map中用作密鑰,其中每個條目記錄相同密鑰的禁用與啓用事件的計數。.filter()調用將啓用禁用和啓用事件計數相同(可能爲2對2)的情況,並且最終.map()將kay/value數組變回預期格式。