如何为搜索对象数组添加多个键和值

问题描述 投票:0回答:1

我有用于搜索数组中数据的功能代码,它在一个键和一个值上运行得很好。我想要的是具有多个键和值的数组搜索,其工作方式类似于sql中的查询and

我的问题:

如何在我的功能代码中添加多个键和值?数组数据:

[{ "id":1, "name":"John", "addres":{ "code":1, "status":1 } }, { "id":2, "name":"Jery", "addres":{ "code":2, "status":1 } }]

我的功能代码:

function where(prop,value) { var filtered = [],checkStatus = 0; for(var i = 0; i < data.length; i++){ var obj = data[i]; for(var key in obj){ var item = obj[key]; if(typeof(obj[key] == "object")){ if(item[prop] == value){ checkStatus = 1; filtered.push(obj); } if (typeof(value) == "string") { if (item[prop] != undefined && item[prop].toLowerCase().includes(value.toLowerCase())) { checkStatus = 1; filtered.push(obj); } } } } if (checkStatus == 0 && obj[prop] != undefined) { if (obj[prop] == value) { filtered.push(obj); } if (typeof(obj[prop]) == 'string') { if (obj[prop] != undefined && obj[prop].toLowerCase().includes(value.toLowerCase())) { filtered.push(obj); } } } } return filtered; }

where('status',1)时有效,但我想要where(['id','status'],[2,1])

我有用于搜索数组中数据的功能代码,它运行得很好,但是只有一个键和一个值。我想要的是具有多个键和值的数组搜索,其工作方式类似于查询和sql。我的问题...

javascript
1个回答
0
投票
对不起,我尝试并成功了
© www.soinside.com 2019 - 2024. All rights reserved.