3

I'm looking to check an API response for against a set of values but the API response contains some additional info that I'm not interested in checking against

Data to check:

[
    {
        "address": {
            "city": "London",
            "firstLine": "23 High Road",
            "postCode": "WC1 1AA",
            "region": "South East",
            "uniqueIdentifier": 239


        },
        "detail": {
            "leaseholdFreehold": "Freehold",
            "location": "Satisfactory",
            "sector": "Office"

        },
        "valuation": {
            "value": "770000",
            "valuationDate": "2018-03-07",
            "yield": "7.75"
        }
    },
    {
        "address": {
            "city": "Leeds",
            "firstLinePropertyName": "45 Headrow",
            "postCode": "LS2 8AA",
            "region": "North East",
            "uniqueIdentifier": 287
        },
        "detail": {
            "leaseholdFreehold": "Freehold",
            "location": "Good",
            "sector": "Residential"

        },
        "valuation": {
            "value": "88000",
            "valuationDate": "2018-03-07",
            "yield": "8.87"
        }
    }
]

API response:

[
        {
            "address": {
                "city": "London",
                "firstLine": "23 High Road",
                "postCode": "WC1 1AA",
                "region": "South East",
                "uniqueIdentifier": 239
            },
            "detail": {
                "designAndCondition": "",
                "developmentCompletionDate": "0001-01-01",
                "leaseholdFreehold": "Freehold",
                "location": "Satisfactory",
                "sector": "Office"

            },
            "valuation": {
                "value": "770000",
                "valuationDate": "2018-03-07",
                "yield": "7.75"
            },
            "dbIdentifier": 240
        },
        {
            "address": {
                "city": "Leeds",
                "firstLinePropertyName": "11 Main Road",
                "postCode": "LS2 8AA",
                "region": "North East",
                "uniqueIdentifier": 282
            },
            "detail": {
                "designAndCondition": "",
                "developmentCompletionDate": "0001-01-01",
                "leaseholdFreehold": "Freehold",
                "location": "Good",
                "sector": "Residential"

            },
            "valuation": {
                "value": "88000",
                "valuationDate": "2018-03-07",
                "yield": "8.75"
            },
            "dbIdentifier": 239
        }
]

So I'm not interested in what values are returned for dbIdentifier, designAndCondition and developmentCompletionDate as they are not in my data to check against but I would like to compare the values for the rest of the properties. In practice these arrays will have more than 2 items

I was initially thinking I would remove the unwanted properties from the objects using the function below

const newArray = responseBody.map(({ dbIdentifierIdentifier, detail: { designAndCondition, developmentCompletionDate  }, ...rest }) => rest)

Then ordering by address.uniqueIdentifier, converting to JSON strings and comparing the strings but the function above doesn't work with the nested properties as newArray doesn't contain the detail object at all

newArray:

[
        {
            "address": {
                "city": "London",
                "firstLine": "23 High Road",
                "postCode": "WC1 1AA",
                "region": "South East",
                "uniqueIdentifier": 239
            },
            "valuation": {
                "value": "770000",
                "valuationDate": "2018-03-07",
                "yield": "7.75"
            },
            "dbIdentifier": 240
        },
        {
            "address": {
                "city": "Leeds",
                "firstLinePropertyName": "11 Main Road",
                "postCode": "LS2 8AA",
                "region": "North East",
                "uniqueIdentifier": 282
            },
            "valuation": {
                "value": "88000",
                "valuationDate": "2018-03-07",
                "yield": "8.75"
            },
            "dbIdentifier": 239
        }
]

IS it possible to do it the above way by passing a destructured nested object a map function?

DrumBongo
  • 31
  • 1
  • If you're open to use lodash you can deep merge the object in your current approach with [\_.merge](https://lodash.com/docs/4.17.15#merge) and also omit selected properties with [\_.omit](https://lodash.com/docs/4.17.15#omit). – Jonathan Nielsen Nov 15 '21 at 12:24
  • @JonathanNielsen I would prefer a non library solution – DrumBongo Nov 15 '21 at 13:07

1 Answers1

1

One way to remove the unwanted properties from the API response would be to first copy the response into a new array (to preserve the original response), then delete the properties:

const apiResponse = [{
    "address": {
      "city": "London",
      "firstLine": "23 High Road",
      "postCode": "WC1 1AA",
      "region": "South East",
      "uniqueIdentifier": 239
    },
    "detail": {
      "designAndCondition": "",
      "developmentCompletionDate": "0001-01-01",
      "leaseholdFreehold": "Freehold",
      "location": "Satisfactory",
      "sector": "Office"

    },
    "valuation": {
      "value": "770000",
      "valuationDate": "2018-03-07",
      "yield": "7.75"
    },
    "dbIdentifier": 240
  },
  {
    "address": {
      "city": "Leeds",
      "firstLinePropertyName": "11 Main Road",
      "postCode": "LS2 8AA",
      "region": "North East",
      "uniqueIdentifier": 282
    },
    "detail": {
      "designAndCondition": "",
      "developmentCompletionDate": "0001-01-01",
      "leaseholdFreehold": "Freehold",
      "location": "Good",
      "sector": "Residential"

    },
    "valuation": {
      "value": "88000",
      "valuationDate": "2018-03-07",
      "yield": "8.75"
    },
    "dbIdentifier": 239
  }
]

let apiResponseCopy = JSON.parse(JSON.stringify(apiResponse))

var newArray = apiResponseCopy.map(i => {
  delete i.dbIdentifier
  delete i.detail.designAndCondition
  delete i.detail.developmentCompletionDate

  return i
})

console.log(newArray)

Then, you should be able to compare the newArray against your data.

Timur
  • 1,682
  • 1
  • 4
  • 11