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

我有一个如下所示的 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']);

有没有办法做到这一点,如果可以,请帮我提供示例代码


翻阅古今
浏览 99回答 1
1回答

慕姐4208626

要过滤数据,您可以使用filterlodash 库的方法。我附上了一个代码片段。希望这有助于解决问题。const data = [{&nbsp; &nbsp; "id": "66b2bc0b-2486-4bb0-a93c-0337ebe1d647",&nbsp; &nbsp; "company_id": "41c56094-ed7e-4fa3-a83e-2e93c6ea5750",&nbsp; &nbsp; "name": null,&nbsp; &nbsp; "type": null,&nbsp; &nbsp; "model": null,&nbsp; &nbsp; "serial_number": null,&nbsp; &nbsp; "mac_address": null,&nbsp; &nbsp; "firmware_verison": null,&nbsp; &nbsp; "gateway_id": null,&nbsp; &nbsp; "device_code": "ST921WF-001E5E02C002",&nbsp; &nbsp; "location_id": null,&nbsp; &nbsp; "status": null,&nbsp; &nbsp; "updated_at": "2020-11-17T07:05:57.037Z",&nbsp; &nbsp; "created_at": "2020-11-17T07:05:57.037Z",&nbsp; &nbsp; "created_by": null,&nbsp; &nbsp; "updated_by": null&nbsp; },&nbsp; {&nbsp; &nbsp; "id": "975c51f8-a6cb-4701-aaa6-077f8a9974a5",&nbsp; &nbsp; "company_id": "41c56094-ed7e-4fa3-a83e-2e93c6ea5750",&nbsp; &nbsp; "location_id": null,&nbsp; &nbsp; "type": null,&nbsp; &nbsp; "name": null,&nbsp; &nbsp; "model": null,&nbsp; &nbsp; "status": null,&nbsp; &nbsp; "serial_number": null,&nbsp; &nbsp; "mac_address": null,&nbsp; &nbsp; "firmware_verison": null,&nbsp; &nbsp; "gateway_id": null,&nbsp; &nbsp; "device_code": "ST921WF-001E5E02C001",&nbsp; &nbsp; "created_by": null,&nbsp; &nbsp; "updated_by": null,&nbsp; &nbsp; "created_at": "2020-11-17T07:05:57.015Z",&nbsp; &nbsp; "updated_at": "2020-11-17T07:05:57.167Z"&nbsp; },&nbsp; {&nbsp; &nbsp; "id": "c4d5e446-c137-443c-a4d7-2c54c204c018",&nbsp; &nbsp; "company_id": "fe49fc61-729d-4a68-8cef-1a4041ff739e",&nbsp; &nbsp; "name": null,&nbsp; &nbsp; "type": null,&nbsp; &nbsp; "model": null,&nbsp; &nbsp; "serial_number": null,&nbsp; &nbsp; "mac_address": null,&nbsp; &nbsp; "firmware_verison": null,&nbsp; &nbsp; "gateway_id": null,&nbsp; &nbsp; "device_code": "ST921TH-001E5E02C001_Naveen",&nbsp; &nbsp; "location_id": null,&nbsp; &nbsp; "status": "active",&nbsp; &nbsp; "updated_at": "2020-11-18T12:10:56.027Z",&nbsp; &nbsp; "created_at": "2020-11-18T12:10:56.027Z",&nbsp; &nbsp; "created_by": null,&nbsp; &nbsp; "updated_by": null&nbsp; },&nbsp; {&nbsp; &nbsp; "id": "9287ce48-e60b-4e8a-96a5-cbedd1ee3be2",&nbsp; &nbsp; "company_id": "fe49fc61-729d-4a68-8cef-1a4041ff739e",&nbsp; &nbsp; "name": null,&nbsp; &nbsp; "type": "smart-plug",&nbsp; &nbsp; "model": null,&nbsp; &nbsp; "serial_number": null,&nbsp; &nbsp; "mac_address": null,&nbsp; &nbsp; "firmware_verison": null,&nbsp; &nbsp; "gateway_id": null,&nbsp; &nbsp; "device_code": "ST921TH-001E5E02C002",&nbsp; &nbsp; "location_id": null,&nbsp; &nbsp; "status": null,&nbsp; &nbsp; "updated_at": "2020-11-18T12:11:21.867Z",&nbsp; &nbsp; "created_at": "2020-11-18T12:11:21.867Z",&nbsp; &nbsp; "created_by": null,&nbsp; &nbsp; "updated_by": null&nbsp; },&nbsp; {&nbsp; &nbsp; "id": "6bf58e90-6a84-480e-b944-2cabb90f60c9",&nbsp; &nbsp; "company_id": "fe49fc61-729d-4a68-8cef-1a4041ff739e",&nbsp; &nbsp; "name": null,&nbsp; &nbsp; "type": "gateway",&nbsp; &nbsp; "model": null,&nbsp; &nbsp; "serial_number": null,&nbsp; &nbsp; "mac_address": null,&nbsp; &nbsp; "firmware_verison": null,&nbsp; &nbsp; "gateway_id": null,&nbsp; &nbsp; "device_code": "ST921GW-001E5E02C002",&nbsp; &nbsp; "location_id": null,&nbsp; &nbsp; "status": "active",&nbsp; &nbsp; "updated_at": "2020-11-18T12:12:23.431Z",&nbsp; &nbsp; "created_at": "2020-11-18T12:12:23.431Z",&nbsp; &nbsp; "created_by": null,&nbsp; &nbsp; "updated_by": null&nbsp; }];const filteredData = _.filter(data, {&nbsp; status: "active",&nbsp; type: "gateway"})console.log(filteredData);<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript