【问题标题】:Lodash filter given json array with multiple conditionLodash过滤器给定具有多个条件的json数组
【发布时间】:2020-11-19 05:36:59
【问题描述】:

我有一个如下所示的 json 数组,我使用 lodash 进行过滤

const data =  [
    {
      "id": "66b2bc0b-2486-4bb0-a93c-0337ebe1d647",
      "company_id": "41c56094-ed7e-4fa3-a83e-2e93c6ea5750",
      "name": null,
      "type": null,
      "model": null,
      "serial_number": null,
      "mac_address": null,
      "firmware_verison": null,
      "gateway_id": null,
      "device_code": "ST921WF-001E5E02C002",
      "location_id": null,
      "status": null,
      "updated_at": "2020-11-17T07:05:57.037Z",
      "created_at": "2020-11-17T07:05:57.037Z",
      "created_by": null,
      "updated_by": null
    },
    {
      "id": "975c51f8-a6cb-4701-aaa6-077f8a9974a5",
      "company_id": "41c56094-ed7e-4fa3-a83e-2e93c6ea5750",
      "location_id": null,
      "type": null,
      "name": null,
      "model": null,
      "status": null,
      "serial_number": null,
      "mac_address": null,
      "firmware_verison": null,
      "gateway_id": null,
      "device_code": "ST921WF-001E5E02C001",
      "created_by": null,
      "updated_by": null,
      "created_at": "2020-11-17T07:05:57.015Z",
      "updated_at": "2020-11-17T07:05:57.167Z"
    },
    {
      "id": "c4d5e446-c137-443c-a4d7-2c54c204c018",
      "company_id": "fe49fc61-729d-4a68-8cef-1a4041ff739e",
      "name": null,
      "type": null,
      "model": null,
      "serial_number": null,
      "mac_address": null,
      "firmware_verison": null,
      "gateway_id": null,
      "device_code": "ST921TH-001E5E02C001_Naveen",
      "location_id": null,
      "status": "active",
      "updated_at": "2020-11-18T12:10:56.027Z",
      "created_at": "2020-11-18T12:10:56.027Z",
      "created_by": null,
      "updated_by": null
    },
    {
      "id": "9287ce48-e60b-4e8a-96a5-cbedd1ee3be2",
      "company_id": "fe49fc61-729d-4a68-8cef-1a4041ff739e",
      "name": null,
      "type": "smart-plug",
      "model": null,
      "serial_number": null,
      "mac_address": null,
      "firmware_verison": null,
      "gateway_id": null,
      "device_code": "ST921TH-001E5E02C002",
      "location_id": null,
      "status": null,
      "updated_at": "2020-11-18T12:11:21.867Z",
      "created_at": "2020-11-18T12:11:21.867Z",
      "created_by": null,
      "updated_by": null
    },
    {
      "id": "6bf58e90-6a84-480e-b944-2cabb90f60c9",
      "company_id": "fe49fc61-729d-4a68-8cef-1a4041ff739e",
      "name": null,
      "type": "gateway",
      "model": null,
      "serial_number": null,
      "mac_address": null,
      "firmware_verison": null,
      "gateway_id": null,
      "device_code": "ST921GW-001E5E02C002",
      "location_id": null,
      "status": "active",
      "updated_at": "2020-11-18T12:12:23.431Z",
      "created_at": "2020-11-18T12:12:23.431Z",
      "created_by": null,
      "updated_by": null
    }
  ]

我需要根据多个条件过滤数组,例如

从 { status: "active","type": "gateway"}; 的数据中过滤;

它应该返回所有满足这个条件的对象。 where 条件应该是动态的,并且应该接受任何支持的字段进行过滤。我尝试了以下但无法添加多个条件

const filteredData = lodash.find(data, ['status', 'active']);

如果可以的话,有办法吗,请帮我提供示例代码

【问题讨论】:

  • lodash.filter(data, { status: 'active', type: 'gateway' }) 应该可以工作。

标签: javascript ecmascript-6 lodash


【解决方案1】:

为了过滤数据,您可以使用 lodash 库的filter 方法。我附上了一个代码sn-p。希望这有助于解决问题。

const data = [{
    "id": "66b2bc0b-2486-4bb0-a93c-0337ebe1d647",
    "company_id": "41c56094-ed7e-4fa3-a83e-2e93c6ea5750",
    "name": null,
    "type": null,
    "model": null,
    "serial_number": null,
    "mac_address": null,
    "firmware_verison": null,
    "gateway_id": null,
    "device_code": "ST921WF-001E5E02C002",
    "location_id": null,
    "status": null,
    "updated_at": "2020-11-17T07:05:57.037Z",
    "created_at": "2020-11-17T07:05:57.037Z",
    "created_by": null,
    "updated_by": null
  },
  {
    "id": "975c51f8-a6cb-4701-aaa6-077f8a9974a5",
    "company_id": "41c56094-ed7e-4fa3-a83e-2e93c6ea5750",
    "location_id": null,
    "type": null,
    "name": null,
    "model": null,
    "status": null,
    "serial_number": null,
    "mac_address": null,
    "firmware_verison": null,
    "gateway_id": null,
    "device_code": "ST921WF-001E5E02C001",
    "created_by": null,
    "updated_by": null,
    "created_at": "2020-11-17T07:05:57.015Z",
    "updated_at": "2020-11-17T07:05:57.167Z"
  },
  {
    "id": "c4d5e446-c137-443c-a4d7-2c54c204c018",
    "company_id": "fe49fc61-729d-4a68-8cef-1a4041ff739e",
    "name": null,
    "type": null,
    "model": null,
    "serial_number": null,
    "mac_address": null,
    "firmware_verison": null,
    "gateway_id": null,
    "device_code": "ST921TH-001E5E02C001_Naveen",
    "location_id": null,
    "status": "active",
    "updated_at": "2020-11-18T12:10:56.027Z",
    "created_at": "2020-11-18T12:10:56.027Z",
    "created_by": null,
    "updated_by": null
  },
  {
    "id": "9287ce48-e60b-4e8a-96a5-cbedd1ee3be2",
    "company_id": "fe49fc61-729d-4a68-8cef-1a4041ff739e",
    "name": null,
    "type": "smart-plug",
    "model": null,
    "serial_number": null,
    "mac_address": null,
    "firmware_verison": null,
    "gateway_id": null,
    "device_code": "ST921TH-001E5E02C002",
    "location_id": null,
    "status": null,
    "updated_at": "2020-11-18T12:11:21.867Z",
    "created_at": "2020-11-18T12:11:21.867Z",
    "created_by": null,
    "updated_by": null
  },
  {
    "id": "6bf58e90-6a84-480e-b944-2cabb90f60c9",
    "company_id": "fe49fc61-729d-4a68-8cef-1a4041ff739e",
    "name": null,
    "type": "gateway",
    "model": null,
    "serial_number": null,
    "mac_address": null,
    "firmware_verison": null,
    "gateway_id": null,
    "device_code": "ST921GW-001E5E02C002",
    "location_id": null,
    "status": "active",
    "updated_at": "2020-11-18T12:12:23.431Z",
    "created_at": "2020-11-18T12:12:23.431Z",
    "created_by": null,
    "updated_by": null
  }
];
const filteredData = _.filter(data, {
  status: "active",
  type: "gateway"
})
console.log(filteredData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 2021-07-05
    • 2019-03-02
    • 2021-04-08
    • 2022-01-18
    • 2021-12-10
    相关资源
    最近更新 更多