在数组中搜索值

时间:2014-10-14 18:45:57

标签: javascript jquery arrays

我有这种类型的数据

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

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

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

8 个答案:

答案 0 :(得分:1)

假设数组总是以正确的顺序排列,你可以进行字符串化比较:

var matchedRooms = rooms.filter(function(room) {
    return JSON.stringify(room.memberID) === JSON.stringify(test);
});

答案 1 :(得分:1)

var selection ='';
rooms.forEach(function(element,index) {

   if(element.memberID==test) {
       selection=element;
    } else {
       return console.log("test has not been found");
    }

});

答案 2 :(得分:1)

var result = $.map(rooms, function(i) {
   if (i.memberID[0] === test[0] && i.memberID[1] == test[1] ) return i; 
}).pop();

console.log(result.roomID); // 'room2'

答案 3 :(得分:1)

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

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

// iterate over each element in the array
for (var i = 0; i < rooms.length i++){
  // look for the entry with a matching `code` value
  if (rooms[i].memberID[0] === test[0] && rooms[i].memberID[1] === test[1]){
     // we found it
    // rooms[i] is the matched result
    // break if need only first match;
  }
}

答案 4 :(得分:0)

不幸的是,您可能需要手动搜索它:

var x;
for(x=0; x<rooms.length; x++){
  if(rooms[x].memberID[0] === test[0] && rooms[x].memberID[1] === test[1]){ 
    // arrays aren't equal, so compare the members
    break;
  }
}

具有rooms等效于测试的.memberID数组的索引现在将存储在x

答案 5 :(得分:0)

您可以按如下方式使用.filter.indexOf

var mRoomIDs = $.map(rooms.filter(function(room) {
    return room.memberID.indexOf( test[0] ) > -1 && room.memberID.indexOf( test[1] ) > -1;
}), function(v,i) {
    return v.roomID;
});

DEMO

Output: ["room1", "room2"]

答案 6 :(得分:0)

尝试

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())))
});

&#13;
&#13;
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))
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 7 :(得分:0)

我终于设法在测试阵列的两个条件下获得房间。

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

//var test = ['1111','2222'];
var currentRoom ;
 for (var i = 0; i < rooms.length ;i++){
            // look for the entry with a matching `code` value
            if (((rooms[i].memberID[0] === test[0])||(rooms[i].memberID[0] === test[1])) && ((rooms[i].memberID[1] === test[1])||(rooms[i].memberID[1] === test[0]))){

                currentRoom=rooms[i]
            }
        }
console.log(currentRoom)

http://jsfiddle.net/t4nmj3nx/3/