为了账号安全,请及时绑定邮箱和手机立即绑定

Lodash过滤器给定具有多个条件的json数组

Lodash过滤器给定具有多个条件的json数组

翻阅古今 2023-08-18 14:28:50
我有一个如下所示的 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    },  ]我需要根据多个条件过滤数组,例如从数据中过滤 { status: "active","type": "gateway"};它应该返回满足此条件的所有对象。where 条件应该是动态的,并且应该接受任何支持的字段进行过滤。我尝试了以下但无法添加多个条件const filteredData = lodash.find(data, ['status', 'active']);有没有办法做到这一点,如果可以,请帮我提供示例代码
查看完整描述

1 回答

?
慕姐4208626

TA贡献1852条经验 获得超7个赞

要过滤数据,您可以使用filterlodash 库的方法。我附上了一个代码片段。希望这有助于解决问题。


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>


查看完整回答
反对 回复 2023-08-18
  • 1 回答
  • 0 关注
  • 94 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信