2015-09-27 75 views
0

我想創建一個數組,這個數組總結了另一個數組。我已經收到了一些很好的想法here,他們都在工作,但他們都產生了另一個障礙,我再次似乎無法解決。刪除對象數組中的空對象鍵

基於@ kooiinc的answer,我當前的代碼看起來是這樣的:

var grants = [ 
    { id: "p_1", location: "loc_1", type: "A", funds: "5000" }, 
    { id: "p_2", location: "loc_2", type: "B", funds: "2000" }, 
    { id: "p_3", location: "loc_3", type: "C", funds: "500" }, 
    { id: "p_2", location: "_ibid", type: "D", funds: "1000" }, 
    { id: "p_2", location: "_ibid", type: "E", funds: "3000" } 
]; 
var projects = []; 
grants.map(
function (v) { 
    if (!(v.id in this)) { 
     this[v.id] = v; 
     projects.push(v); 
    } else { 
     var current = this[v.id]; 
     current.type = [v.type].concat(current.type); 
     current.funds = [v.funds].concat(current.funds); 
    } 
}, {}); 

...這樣做具有以下期望的結果(typefunds加入到子陣列外,其餘都推不變):

[ 
    { "id": "p_1", "location": "loc_1", "type": "A", "funds": "5000" }, 
    { "id": "p_2", "location": "loc_2", "type": ["E","D","B"], "funds": ["3000","1000","2000"] }, 
    { "id": "p_3", "location": "loc_3", "type": "C", "funds": "500" } 
] 

但是,如果某些對象有一些未定義的鍵值,結果將在數組中有空值。例如像這樣的(看type陣列):

[ 
    { "id": "p_1", "location": "loc_1", "type": "A", "funds": "5000" }, 
    { "id": "p_2", "location": "loc_2", "type": ["E",null,null], "funds": ["3000","1000","2000"] }, 
    { "id": "p_3", "location": "loc_3", "type": "C", "funds": "500" } 
] 

(這裏有一個fiddle具有同樣的事情。)

我試圖找到後刪除這些的快捷方式(如herehere),但由於某種原因,沒有任何常規方法(遞歸刪除所有未定義的鍵/空值)似乎工作,無論我將它們放在我的代碼中。他們不會給出錯誤,他們只是不會刪除任何東西。

是否有可能以某種方式排除映射中未定義的鍵?

更新:所以有些對象鍵將不會有任何,只是[null,null,null]而其他人將有一些但不是所有喜歡["E",null,null]。這個想法是刪除所有的空項目,如果沒有剩下任何東西,然後刪除對象的關鍵。

回答

2

試試這樣說:

grants.map(
function (v) { 
    if (!(v.id in this)) { 
     // => initialize if empty 
     v.type = v.type || []; 
     v.funds = v.funds || []; 
     this[v.id] = v; 
     projects.push(v); 
    } else { 
     var current = this[v.id]; 
     current.type = v.type ? [v.type].concat(current.type) : current.type; 
     current.funds = v.funds ? [v.funds].concat(current.funds) : current.funds; 
    } 
}, {}); 

現在,空值將不會在結果中顯示出來。

+0

是的,我認爲這太,但這似乎並沒有刪除所有的空,在這裏看到:http://jsfiddle.net/eebgasw6/1/ – tmr

+0

我明白了,有一個非常簡單的解決方案。請參閱編輯答案 – KooiInc

0

我認爲你可以測試typefunds屬性的出現,只有存在,然後插入或更新元素。

a.type && a.funds && ... 

var grants = [ 
 
     { id: "p_1", location: "loc_1", type: "A", funds: "5000" }, 
 
     { id: "p_2", location: "loc_2", funds: "2000" }, 
 
     { id: "p_3", location: "loc_3", type: "C", funds: "500" }, 
 
     { id: "p_2", location: "_ibid", funds: "1000" }, 
 
     { id: "p_2", location: "_ibid", type: "E", funds: "3000" } 
 
    ], 
 
    project = []; 
 

 
grants.forEach(function (a) { 
 
    a.type && a.funds && !project.some(function (b, i) { 
 
     if (a.id === b.id) { 
 
      project[i].type.push(a.type); 
 
      project[i].funds.push(a.funds); 
 
      return true; 
 
     } 
 
    }) && project.push({ id: a.id, location: a.location, type: [a.type], funds: [a.funds] }); 
 
}); 
 
document.write('<pre>' + JSON.stringify(project, 0, 4) + '</pre>');

+0

問題在於,我失去了加入我想加入的能力(在本例中爲「type」和「funds」)。 – tmr