2015-05-22 42 views
1

我想知道最簡單的javascript方法,用於從大型GeoJSON集合(大約10萬行)中刪除重複值(座標)。刪除重複值後,我想將更新後的集合記錄到控制檯或在網頁上顯示結果。下面是我的嘗試示例,但是我在控制檯中獲取的是一個空數組。從GeoJSON集合中刪除重複的值

window.onload = init; 

function init() { 

function eliminateDuplicates(arr) { 
    var i; 
    var len = arr.length; 
    var out = []; 
    var obj = {}; 

    for (i = 0; i < len; i++) { 
    obj[arr[i]]=0; 
    } 
    for (i in obj) { 
    out.push(i); 
    } 
    return out; 
}  

var newCollection = eliminateDuplicates(arr); 
console.log(newCollection); 

} 

var arr = 
{ 
     "type": "FeatureCollection", 
     "features": [ 
      { 
       "type": "Feature","properties": {"@id": "123", 
       "name": "test1", 
       "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."}, 

       "geometry": { 
        "type": "Point","coordinates": [-73.994720, 40.686902] 
       } 
      }, 
      { 
       "type": "Feature","properties": {"@id": "1234", 
       "name": "test2", 
       "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."}, 

       "geometry": { 
        "type": "Point","coordinates": [-73.994720, 40.686902] 
       } 
      }, 
      { 
       "type": "Feature","properties": {"@id": "1945", 
       "name": "test3", 
       "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."}, 

       "geometry": { 
        "type": "Point","coordinates": [-73.989205, 40.686675] 
       } 
      }, 
      { 
       "type": "Feature","properties": {"@id": "1946", 
       "name": "test3", 
       "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."}, 

       "geometry": { 
        "type": "Point","coordinates": [-73.994655, 40.687391]    
       } 
      }, 
      { 
       "type": "Feature","properties": {"@id": "1947", 
       "name": "test4", 
       "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."}, 

       "geometry": { 
        "type": "Point","coordinates": [-73.985557, 40.687683]    
       } 
      } 
    ]   

} 
+0

你們是不是要消除同一座標的項目? –

回答

0

您將collection作爲數組處理,但是它是Object。

function eliminateDuplicates(collection) { 
    var i; 
    var len = collection.features.length; 
    var out = []; 
    var obj = {}; 

    /* Your deduplication code over collection.features here */ 
} 
4

這裏假定重複你的意思是一個具有相同座標的條目。

// for simplicity's sake I got rid of the outer data layer 
 
// and made arr a simple array. With the original data you 
 
// could do arr.features.forEach instead of the arr.forEach below. 
 
var arr = [ 
 
      { 
 
       "type": "Feature","properties": {"@id": "123", 
 
       "name": "test1", 
 
       "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."}, 
 

 
       "geometry": { 
 
        "type": "Point","coordinates": [-73.994720, 40.686902] 
 
       } 
 
      }, 
 
      { 
 
       "type": "Feature","properties": {"@id": "1234", 
 
       "name": "test2", 
 
       "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."}, 
 

 
       "geometry": { 
 
        "type": "Point","coordinates": [-73.994720, 40.686902] 
 
       } 
 
      }, 
 
      { 
 
       "type": "Feature","properties": {"@id": "1945", 
 
       "name": "test3", 
 
       "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."}, 
 

 
       "geometry": { 
 
        "type": "Point","coordinates": [-73.989205, 40.686675] 
 
       } 
 
      }, 
 
      { 
 
       "type": "Feature","properties": {"@id": "1946", 
 
       "name": "test3", 
 
       "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."}, 
 

 
       "geometry": { 
 
        "type": "Point","coordinates": [-73.994655, 40.687391]    
 
       } 
 
      }, 
 
      { 
 
       "type": "Feature","properties": {"@id": "1947", 
 
       "name": "test4", 
 
       "description": "Lorem Ipsum is simply dummy text of the printing and typesetting industry."}, 
 

 
       "geometry": { 
 
        "type": "Point","coordinates": [-73.985557, 40.687683]    
 
       } 
 
      } 
 
    ]; 
 

 
// generates a key from the item's coordinates. 
 
function keyFor(item) { 
 
    return item.geometry.coordinates[0] + ':' + item.geometry.coordinates[1]; 
 
} 
 

 
// index the entries by their coordinates such that 
 
// indexed['lat:lng'] == the entry. 
 
var indexed = {}; 
 
arr.forEach(function(item) { 
 
    // a duplicate key will replace the existing entry 
 
    indexed[keyFor(item)] = item; 
 
}); 
 

 
// turn the results back into an array of just values. 
 
var result = Object.keys(indexed).map(function(k) { 
 
    return indexed[k]; 
 
}); 
 

 
// emit the results. 
 
console.log(result);

+0

這工作謝謝! – teslorg

+0

太棒了!如果您將此標記爲接受的答案,我將不勝感激! – ray