我有这类数据

var rooms = [
       {roomID:'room1',memberID:['1111','2222']},
       {roomID:'room2',memberID:['1111','3333']},
       {roomID:'room2',memberID:['3333','1111']}]

var test = ['1111','2222']


要么
 var test = ['2222','1111']
我想选择memeberID等于测试的房间,即应同时为两个测试值选择room1
请帮忙。

最佳答案

尝试

var test = ["1111","2222"] // , test2 = ["2222", "1111"];
// `test2 === test.reverse()` -> utilized same variable (`test`) for OR check
var res = $.grep(rooms, function(v, k) {
  return (String(v.memberID) === (String(test) || String(test.reverse())))
});




var rooms = [
       {roomID:'room1',memberID:['1111','2222']},
       {roomID:'room2',memberID:['1111','3333']},
       {roomID:'room2',memberID:['3333','1111']}]

, test = ['1111','2222'] //, test2 = ["2222", "1111"]

, res = $.grep(rooms, function(v, k) {
  return (String(v.memberID) === (String(test) || String(test.reverse())))
});
console.log(res);
document.write(JSON.stringify(res))

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

关于javascript - 在数组中搜索值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26368084/

10-11 16:21