John Doi
John Doi

Reputation: 23

Removing matching elements in a JSON object

I have the following JSON object. I would like to go through it and if the element has a matching co-ordinate, delete the copy and retain the original. I've tried using .filter to strip away the copies but i'm unable to get it working. What would be the best way to strip away copies?

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"
            }
        }
    ],

}

Would become the following with just the original rather than a mix of copies.

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"
            }
        }
    ],

}

Attempt using set, it just returns the exact object passed to it

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

Upvotes: 1

Views: 864

Answers (3)

Jaromanda X
Jaromanda X

Reputation: 1

A simple filter using findIndex should be enough

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)

Upvotes: 0

zfrisch
zfrisch

Reputation: 8670

Solution

You can do this with Array.prototype.filter by destructuring down to coordinates and checking a locally scoped Set for the stringified array.


Code:

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

Commented Code:

  // 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)

Working Code:

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);

Upvotes: 1

etarhan
etarhan

Reputation: 4176

I would recommend using a library like lodash to make your life easier. In particular the uniqBy function of lodash. I have deduplicated based on matching coordinates. Full working example below:

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>

Upvotes: 0

Related Questions