删除JSON对象中的匹配元素

时间:2019-04-03 22:46:59

标签: javascript json

我有以下JSON对象。我想浏览一下,如果元素具有匹配的坐标,则删除副本并保留原始副本。我尝试使用.filter剥离副本,但无法正常工作。剥离副本的最佳方法是什么?

business1 {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    12.564111,
                    55.675659
                ]
            },
            "place_name": "Axeltorv 3, 1609 København, Denmark",
            "properties": {
                "title": "Comparison of Alternative Meat Inspection Regimes for Pigs From Non-Controlled Housing ? Considering the Cost of Error",
                "countries": "Denmark",
                "authorTitle": "Lis Alban",
                "businessName": "Danish Agriculture and Food Council"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    12.564111,
                    55.675659
                ]
            },
            "place_name": "Axeltorv 3, 1609 København, Denmark",
            "properties": {
                "title": "Comparison of Alternative Meat Inspection Regimes for Pigs From Non-Controlled Housing ? Considering the Cost of Error",
                "countries": "Denmark",
                "authorTitle": "Lisbeth Harm Nielsen",
                "businessName": "Danish Agriculture and Food Council"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -1.26288597488275,
                    51.7576388596821
                ]
            },
            "place_name": "OX1 2JD, Oxford, Oxfordshire, England, United Kingdom",
            "properties": {
                "title": "ENSO Drives interannual variation of forest woody growth across the tropics",
                "countries": "United Kingdom",
                "authorTitle": "C?cile A J Girardin",
                "businessName": "University of Oxford"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -1.26288597488275,
                    51.7576388596821
                ]
            },
            "place_name": "OX1 2JD, Oxford, Oxfordshire, England, United Kingdom",
            "properties": {
                "title": "ENSO Drives interannual variation of forest woody growth across the tropics",
                "countries": "United Kingdom",
                "authorTitle": "Cecilia A L Dahlsj?",
                "businessName": "University of Oxford"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -111.65511,
                    35.19363
                ]
            },
            "place_name": "1899 PO Box, Flagstaff, Arizona 86011, United States",
            "properties": {
                "title": "ENSO Drives interannual variation of forest woody growth across the tropics",
                "countries": "United States",
                "authorTitle": "Christopher E Doughty",
                "businessName": "Northern Arizona University"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -1.26288597488275,
                    51.7576388596821
                ]
            },
            "place_name": "OX1 2JD, Oxford, Oxfordshire, England, United Kingdom",
            "properties": {
                "title": "ENSO Drives interannual variation of forest woody growth across the tropics",
                "countries": "United Kingdom",
                "authorTitle": "Erika Berenguer",
                "businessName": "University of Oxford"
            }
        }
    ],

}

只有原始版本而不是混合版本的副本会成为跟随者。

business1 {
    "type": "FeatureCollection",
    "features": [
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    12.564111,
                    55.675659
                ]
            },
            "place_name": "Axeltorv 3, 1609 København, Denmark",
            "properties": {
                "title": "Comparison of Alternative Meat Inspection Regimes for Pigs From Non-Controlled Housing ? Considering the Cost of Error",
                "countries": "Denmark",
                "authorTitle": "Lis Alban",
                "businessName": "Danish Agriculture and Food Council"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -1.26288597488275,
                    51.7576388596821
                ]
            },
            "place_name": "OX1 2JD, Oxford, Oxfordshire, England, United Kingdom",
            "properties": {
                "title": "ENSO Drives interannual variation of forest woody growth across the tropics",
                "countries": "United Kingdom",
                "authorTitle": "Cecilia A L Dahlsj?",
                "businessName": "University of Oxford"
            }
        },
        {
            "type": "Feature",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -111.65511,
                    35.19363
                ]
            },
            "place_name": "1899 PO Box, Flagstaff, Arizona 86011, United States",
            "properties": {
                "title": "ENSO Drives interannual variation of forest woody growth across the tropics",
                "countries": "United States",
                "authorTitle": "Christopher E Doughty",
                "businessName": "Northern Arizona University"
            }
        }
    ],

}

尝试使用set,它只是返回传递给它的确切对象

 uniqueArray = [...new Set(business1.features)]

3 个答案:

答案 0 :(得分:1)

解决方案

您可以使用Array.prototype.filter进行此操作,方法是分解为坐标并检查字符串数组的局部作用域Set


代码:

business1.features.filter(({geometry:{ coordinates }}) => 
  !b.has(s(coordinates)) && (b.add(s(coordinates)), true), 
  b = new Set(), s = JSON.stringify)

注释代码:

  // Get the features array
  business1.features

  // call filter on the array
  .filter(

  // destructure each feature down to "coordinates"
  ({geometry:{ coordinates }}) => 

  /* check if the local set object contains stringified coordinates
  if it does not - 
   - we add it to the set and respond true
   - it is added to the filtered array
  if it does - 
    - we respond false
    - it is not added to the filtered array.
  */
  !b.has(s(coordinates)) && (b.add(s(coordinates)), true), 

  // declare our local variables for our Set and stringify:
  b = new Set(), s = JSON.stringify)

工作代码:

let business1={type:"FeatureCollection",features:[{type:"Feature",geometry:{type:"Point",coordinates:[12.564111,55.675659]},place_name:"Axeltorv 3, 1609 København, Denmark",properties:{title:"Comparison of Alternative Meat Inspection Regimes for Pigs From Non-Controlled Housing ? Considering the Cost of Error",countries:"Denmark",authorTitle:"Lis Alban",businessName:"Danish Agriculture and Food Council"}},{type:"Feature",geometry:{type:"Point",coordinates:[12.564111,55.675659]},place_name:"Axeltorv 3, 1609 København, Denmark",properties:{title:"Comparison of Alternative Meat Inspection Regimes for Pigs From Non-Controlled Housing ? Considering the Cost of Error",countries:"Denmark",authorTitle:"Lisbeth Harm Nielsen",businessName:"Danish Agriculture and Food Council"}},{type:"Feature",geometry:{type:"Point",coordinates:[-1.26288597488275,51.7576388596821]},place_name:"OX1 2JD, Oxford, Oxfordshire, England, United Kingdom",properties:{title:"ENSO Drives interannual variation of forest woody growth across the tropics",countries:"United Kingdom",authorTitle:"C?cile A J Girardin",businessName:"University of Oxford"}},{type:"Feature",geometry:{type:"Point",coordinates:[-1.26288597488275,51.7576388596821]},place_name:"OX1 2JD, Oxford, Oxfordshire, England, United Kingdom",properties:{title:"ENSO Drives interannual variation of forest woody growth across the tropics",countries:"United Kingdom",authorTitle:"Cecilia A L Dahlsj?",businessName:"University of Oxford"}},{type:"Feature",geometry:{type:"Point",coordinates:[-111.65511,35.19363]},place_name:"1899 PO Box, Flagstaff, Arizona 86011, United States",properties:{title:"ENSO Drives interannual variation of forest woody growth across the tropics",countries:"United States",authorTitle:"Christopher E Doughty",businessName:"Northern Arizona University"}},{type:"Feature",geometry:{type:"Point",coordinates:[-1.26288597488275,51.7576388596821]},place_name:"OX1 2JD, Oxford, Oxfordshire, England, United Kingdom",properties:{title:"ENSO Drives interannual variation of forest woody growth across the tropics",countries:"United Kingdom",authorTitle:"Erika Berenguer",businessName:"University of Oxford"}}]};



let result = business1.features.filter(({geometry:{ coordinates }}) => 
  !b.has(s(coordinates)) && (b.add(s(coordinates)), true), b = new Set(), s = JSON.stringify)

console.log(result);

答案 1 :(得分:0)

我建议您使用lodash之类的库来简化生活。特别是lodash的uniqBy函数。我已经根据匹配的坐标进行了重复数据删除。完整的工作示例如下:

const featureCollection = {
  "type": "FeatureCollection",
  "features": [{
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          12.564111,
          55.675659
        ]
      },
      "place_name": "Axeltorv 3, 1609 København, Denmark",
      "properties": {
        "title": "Comparison of Alternative Meat Inspection Regimes for Pigs From Non-Controlled Housing ? Considering the Cost of Error",
        "countries": "Denmark",
        "authorTitle": "Lis Alban",
        "businessName": "Danish Agriculture and Food Council"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [
          12.564111,
          55.675659
        ]
      },
      "place_name": "Axeltorv 3, 1609 København, Denmark",
      "properties": {
        "title": "Comparison of Alternative Meat Inspection Regimes for Pigs From Non-Controlled Housing ? Considering the Cost of Error",
        "countries": "Denmark",
        "authorTitle": "Lisbeth Harm Nielsen",
        "businessName": "Danish Agriculture and Food Council"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [-1.26288597488275,
          51.7576388596821
        ]
      },
      "place_name": "OX1 2JD, Oxford, Oxfordshire, England, United Kingdom",
      "properties": {
        "title": "ENSO Drives interannual variation of forest woody growth across the tropics",
        "countries": "United Kingdom",
        "authorTitle": "C?cile A J Girardin",
        "businessName": "University of Oxford"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [-1.26288597488275,
          51.7576388596821
        ]
      },
      "place_name": "OX1 2JD, Oxford, Oxfordshire, England, United Kingdom",
      "properties": {
        "title": "ENSO Drives interannual variation of forest woody growth across the tropics",
        "countries": "United Kingdom",
        "authorTitle": "Cecilia A L Dahlsj?",
        "businessName": "University of Oxford"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [-111.65511,
          35.19363
        ]
      },
      "place_name": "1899 PO Box, Flagstaff, Arizona 86011, United States",
      "properties": {
        "title": "ENSO Drives interannual variation of forest woody growth across the tropics",
        "countries": "United States",
        "authorTitle": "Christopher E Doughty",
        "businessName": "Northern Arizona University"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Point",
        "coordinates": [-1.26288597488275,
          51.7576388596821
        ]
      },
      "place_name": "OX1 2JD, Oxford, Oxfordshire, England, United Kingdom",
      "properties": {
        "title": "ENSO Drives interannual variation of forest woody growth across the tropics",
        "countries": "United Kingdom",
        "authorTitle": "Erika Berenguer",
        "businessName": "University of Oxford"
      }
    }
  ],
};

featureCollection.features = _.uniqBy(featureCollection.features, (b) => b.geometry.coordinates[0] && b.geometry.coordinates[1]);
console.log(featureCollection);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

答案 2 :(得分:0)

使用findIndex的简单过滤器就足够了

const business1 = {"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[12.564111,55.675659]},"place_name":"Axeltorv 3, 1609 København, Denmark","properties":{"title":"Comparison of Alternative Meat Inspection Regimes for Pigs From Non-Controlled Housing ? Considering the Cost of Error","countries":"Denmark","authorTitle":"Lis Alban","businessName":"Danish Agriculture and Food Council"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[12.564111,55.675659]},"place_name":"Axeltorv 3, 1609 København, Denmark","properties":{"title":"Comparison of Alternative Meat Inspection Regimes for Pigs From Non-Controlled Housing ? Considering the Cost of Error","countries":"Denmark","authorTitle":"Lisbeth Harm Nielsen","businessName":"Danish Agriculture and Food Council"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-1.26288597488275,51.7576388596821]},"place_name":"OX1 2JD, Oxford, Oxfordshire, England, United Kingdom","properties":{"title":"ENSO Drives interannual variation of forest woody growth across the tropics","countries":"United Kingdom","authorTitle":"C?cile A J Girardin","businessName":"University of Oxford"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-1.26288597488275,51.7576388596821]},"place_name":"OX1 2JD, Oxford, Oxfordshire, England, United Kingdom","properties":{"title":"ENSO Drives interannual variation of forest woody growth across the tropics","countries":"United Kingdom","authorTitle":"Cecilia A L Dahlsj?","businessName":"University of Oxford"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-111.65511,35.19363]},"place_name":"1899 PO Box, Flagstaff, Arizona 86011, United States","properties":{"title":"ENSO Drives interannual variation of forest woody growth across the tropics","countries":"United States","authorTitle":"Christopher E Doughty","businessName":"Northern Arizona University"}},{"type":"Feature","geometry":{"type":"Point","coordinates":[-1.26288597488275,51.7576388596821]},"place_name":"OX1 2JD, Oxford, Oxfordshire, England, United Kingdom","properties":{"title":"ENSO Drives interannual variation of forest woody growth across the tropics","countries":"United Kingdom","authorTitle":"Erika Berenguer","businessName":"University of Oxford"}}]};

business1.features = business1.features.filter(
    ({geometry:{coordinates:[lat,lon]}}, index, array) => 
        array.findIndex(({geometry:{coordinates:[slat,slon]}}) => 
            lat === slat && lon === slon) === index
);


console.log(business1)