亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 190 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號