删除JSON对象中的匹配元素

问题描述 投票:1回答:3

我有以下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)]
javascript json
3个回答
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);

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>

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)
© www.soinside.com 2019 - 2024. All rights reserved.