首页 > 解决方案 > Lodash 将 JSON 数组中的重复项删除到对象

问题描述

我有以下 JSON 数据并试图删除重复的记录,并希望使用 lodash 将其变为单个对象,但没有帮助。

const final = [
  {
    "student_id": 1,
    "name": "A",
    "student_img": {
      "image_id": 123
    },
    "policy": {
      "policy_id": 2,
      "entity_id": 52,
      "policy_name": "test",
      "blocks": {
        "block_id": 1,
        "block_name": "Block A"
      }
    }
  },
  {
    "student_id": 1,
    "name": "A",
    "student_img": {
      "image_id": 123
    },
    "policy": {
      "policy_id": 2,
      "entity_id": 52,
      "policy_name": "test",
      "blocks": {
        "block_id": 2,
        "block_name": "Block B"
      }
    }
  }
]

我尝试使用 lodash 删除重复的内容uniqWith,如下所示

_.uniqWith(final, _.isEqual);

但它不工作

Expecting following result

{
    "student_id": 1,
    "name": "A",
    "student_img": {
      "image_id": 123,
    },
    "policy": {
      "policy_id": 2,
      "entity_id": 52,
      "policy_name": "test",
      "blocks": [
        {
          "block_id": 1,
          "block_name": "Block A"
        },
        {
          "block_id": 2,
          "block_name": "Block B"
        }
      ]
    },

  }

请帮忙

标签: javascriptlodash

解决方案


推荐阅读