2016-03-10 39 views
-1

我有兩個user_id。我需要弄清楚兩個user_ids是否已經屬於一個對話。查找嵌套javascript數組中的匹配值

假設我想搜索user_id 7和4之間的對話,下面的數組會返回空白,因爲7和4不屬於對話。如果我搜索7和8,它會返回一個數組。

我打開lodash和一個班輪的例子。

[ 
    [{ 
     "full_name": "Bob", 
     "id": 7, 
     "user_conversations": { 
      "user_id": 7, 
      "conversation_id": 1 
     } 
    }, { 
     "full_name": "Sally", 
     "id": 8, 
     "user_conversations": { 
      "user_id": 8, 
      "conversation_id": 1 
     } 
    }], 
    [{ 
     "full_name": "Suzy", 
     "id": 6, 
     "user_conversations": { 
      "user_id": 6, 
      "conversation_id": 2 
     } 
    }, { 
     "full_name": "Bob", 
     "id": 7, 
     "user_conversations": { 
      "user_id": 7, 
      "conversation_id": 2 
     } 
    }] 
] 
+0

回報什麼的陣列? – Marty

+1

遍歷數組,創建7的對話數組,以及4的對話數組。然後查看兩個數組是否有任何共同的值。 – Barmar

回答

0
var data = [ 
    [{ 
     "full_name": "Bob", 
     "id": 7, 
     "user_conversations": { 
      "user_id": 7, 
      "conversation_id": 1 
     } 
    }, { 
     "full_name": "Sally", 
     "id": 8, 
     "user_conversations": { 
      "user_id": 8, 
      "conversation_id": 1 
     } 
    }], 
    [{ 
     "full_name": "Suzy", 
     "id": 6, 
     "user_conversations": { 
      "user_id": 6, 
      "conversation_id": 2 
     } 
    }, { 
     "full_name": "Bob", 
     "id": 7, 
     "user_conversations": { 
      "user_id": 7, 
      "conversation_id": 2 
     } 
    }] 
]; 

var relations = {}; 

var addRelation = function(pers1, pers2, conv_id) { 
    if (!relations[pers1]) { 
     relations[pers1] = {}; 
    } 
    if (!relations[pers1][pers2]) { 
     relations[pers1][pers2] = []; 
    } 
    relations[pers1][pers2].push(conv_id); 
}; 

var checkForRelation = function(pers1, pers2) { 
    if (relations[pers1] && relations[pers1][pers2]) { 
     return relations[pers1][pers2]; 
    } else { 
     return []; 
    } 
}; 

for (var i = 0; i < data.length; i++) { 
    var tmp = data[i]; 
    var conv_id = tmp[0].user_conversation.conversation_id; 
    var pers1 = tmp[0].id; 
    var pers2 = tmp[1].id; 

    addRelation(pers1, pers2, conv_id); 
    addRelation(pers2, pers1, conv_id); 
} 
  1. 創建對象舉行對話 ​​'關係' 爲每個用戶。
  2. 遍歷數據結構,對於數據數組中的每個數組,創建user1和user2之間的關聯,反之亦然。

所以關係最終會看起來像這樣:

{ 
    "7": { 
     "8": ['1'], 
     "6": ['2'] 
    }, 
    "8": { 
     "7": ['1'] 
    }, 
    "6": { 
     "7": ['2'] 
    } 
} 

checkForRelations然後少了點人之一,人兩者之間的關係結構的查找。

0

這裏的lodash非常簡潔的版本:

_.pickBy(_.zipObject(_.map(data, '[0]user_conversations.conversation_id'), 
        _.map(data, _.partial(_.map, _, 'id'))), 
     function (p) { 
    return _.intersection(p, [7, 8]).length == 2; 
}); 

說明:

var data = [ 
 
    [{ 
 
    "full_name": "Bob", 
 
    "id": 7, 
 
    "user_conversations": { 
 
     "user_id": 7, 
 
     "conversation_id": 1 
 
    } 
 
    }, { 
 
    "full_name": "Sally", 
 
    "id": 8, 
 
    "user_conversations": { 
 
     "user_id": 8, 
 
     "conversation_id": 1 
 
    } 
 
    }], 
 
    [{ 
 
    "full_name": "Suzy", 
 
    "id": 6, 
 
    "user_conversations": { 
 
     "user_id": 6, 
 
     "conversation_id": 2 
 
    } 
 
    }, { 
 
    "full_name": "Bob", 
 
    "id": 7, 
 
    "user_conversations": { 
 
     "user_id": 7, 
 
     "conversation_id": 2 
 
    } 
 
    }] 
 
]; 
 

 
// get all conversation ids in order 
 
var conversations = _.map(data, '[0]user_conversations.conversation_id'); 
 

 
// get all conversations' participants' user ids in order 
 
var participants = _.map(data, _.partial(_.map, _, 'id')); 
 

 
// create an object that maps conversation ids to their participants' user ids 
 
var convToUsers = _.zipObject(conversations, participants); 
 

 
// now pick the conversations that include the participants you want: 
 
var picked = _.pickBy(convToUsers, function (p) { 
 
    return _.intersection(p, [7, 8]).length == 2; 
 
}); 
 

 
_.forEach(picked, function (parts, conv) { 
 
    document.write('<pre>Conversation ' + conv + ' has participants ' + 
 
     JSON.stringify(parts) + '</pre>'); 
 
});
<script src="https://cdn.jsdelivr.net/lodash/4.6.1/lodash.min.js"></script>

相關問題