【问题标题】:Comparing 2 arrays of nested objects while ignoring some properties in Javascript比较 2 个嵌套对象数组,同时忽略 Javascript 中的某些属性
【发布时间】:2021-11-16 07:44:55
【问题描述】:

我希望根据一组值检查 API 响应,但 API 响应包含一些我不想检查的附加信息

要检查的数据:

[
    {
        "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 响应:

[
        {
            "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
        }
]

所以我对 dbIdentifierdesignAndConditiondevelopmentCompletionDate 返回的值不感兴趣,因为它们不在我要检查的数据中,但我想比较其余属性的值.在实践中,这些数组将有超过 2 个项目

我最初认为我会使用下面的函数从对象中删除不需要的属性

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

然后按address.uniqueIdentifier 排序,转换为JSON 字符串并比较字符串,但上面的函数不适用于嵌套属性,因为newArray 根本不包含detail 对象

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

是否可以通过将解构的嵌套对象传递给映射函数来实现上述方式?

【问题讨论】:

  • 如果您愿意使用 lodash,您可以在当前方法中使用 _.merge 深度合并对象,也可以使用 _.omit 省略选定的属性。
  • @JonathanNielsen 我更喜欢非图书馆解决方案

标签: javascript json


【解决方案1】:

从 API 响应中删除不需要的属性的一种方法是首先将响应复制到新数组中(以保留原始响应),然后删除属性:

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)

然后,您应该能够将 newArray 与您的数据进行比较。

【讨论】:

  • 这可行,但我相信删除对性能有一些影响
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-19
  • 1970-01-01
  • 2021-11-04
  • 2012-04-04
  • 2020-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多