2014-01-10 136 views
1

我正在創建一個聊天腳本,用戶可以在其中設置他們的興趣。當用戶連接至服務器的客戶端發送以下JSON過的WebSocket:查找幾個陣列之間最接近的匹配

{"id": int, "hash": md5, "automessage": {...}, "interests": ["cars", "programming", "stackoverflow"]} 

當接收到第一連接它被推到等待陣列。每當另一個連接開始時,它會從陣列中刪除最後一個對象,將它們「配對」在一起。我現在需要做的是編寫一個函數,以便當它收到消息時,它會查看等待數組中所有對象的興趣值,並返回具有最常見興趣的對象。例如,如果等待陣列看起來像這樣:

[ 
    {"id": int, "hash": md5, "automessage": {...}, "interests": ["cats", "animals", "cars"]}, 
    {"id": int, "hash": md5, "automessage": {...}, "interests": ["programming", "ssh", "stackoverflow"]}, 
    {"id": int, "hash": md5, "automessage": {...}, "interests": ["climbing", "football", "coffee"]} 
] 

現在被接收上述消息時,它看起來通過上述數組,並返回該對象具有最相似的興趣。因此,在這個例子中它將返回{"id": int, "hash": md5, "automessage": {...}, "interests": ["programming", "ssh", "stackoverflow"]}

如果找不到具有相似興趣的任何條目,則應將該用戶添加到等待列表數組中。

我很堅持這個,所以任何人都可以幫忙嗎?


不太確定爲什麼問題被downvoted。謹慎添加評論?

+0

給你一回,因爲這是個有趣的問題,我:) –

回答

1

DEMO

你可以試試這個從等待列表中找到的最佳人選,那麼,如果沒有找到,你可以把它添加到

var incoming = { 
    "id": 'int', 
     "hash": 'md5', 
     "automessage": {}, 
     "interests": ["cars", "programming", "stackoverflow"], 
}; 

var waiting_list = [{ 
    "id": 'int', 
     "hash": 'md5', 
     "automessage": {}, 
     "interests": ["cats", "animals", "cars"] 
}, { 
    "id": 'int', 
     "hash": 'md5', 
     "automessage": {}, 
     "interests": ["programming", "ssh", "stackoverflow"] 
}, { 
    "id": 'int', 
     "hash": 'md5', 
     "automessage": {}, 
     "interests": ["climbing", "football", "coffee"] 
}]; 

// var exists = (myNumbers.indexOf(bar) > -1); //true 

var largerCount = 0, index; // will contain the count & index of largest match 
for (var i = 0; i < waiting_list.length; i++) { // iterate over the waiting list 
    var current = waiting_list[i]; 
    var currentCount = 0; // get the current match count 
    var incoming_array = incoming.interests; // get the incoming interest 
    for (var j = 0; j < incoming_array.length; j++) { 

     if(current.interests.indexOf(incoming_array[j]) > -1) { 
      currentCount++; // add to count if match is found 
     } 
     if(currentCount > largerCount) { // if current count is bigger then assign it to largerCounr 
      largerCount = currentCount; 
      index = i; // assign the index of match 
     } 
    } 
    currentCount = 0; 
} 

if(index >= 0) { 
console.log(waiting_list[index]); // print the match 
} else { 
    // add to waiting list 
    waiting_list.push(incoming); 
} 
+0

不錯,但有一個問題,如果'(指數)'因爲如果'指數= 0'(這是可能),那麼'if'對待這是錯誤的。快速演示http://jsfiddle.net/BenedictLewis/j3VWk/ –

+0

感謝您的更新。您可以將其更改爲索引> = 0,它應該可以工作。將更新答案以及 –

+0

謝謝。我發現'index!= null'也可以工作(http://stackoverflow.com/a/5113396/2078412) –

2

我會回答「找到最接近元件」的部分:

function intersection(a, b) { 
    return a.filter(function(x) { return b.indexOf(x) >= 0 }) 
} 

function closest(ary, arrays) { 
    return arrays.map(function(x) { 
     return [intersection(ary, x).length, x] 
    }).sort(function(a, b) { 
     return b[0] - a[0] 
    })[0][1] 
} 

實施例:

me = ["cars", "programming", "stackoverflow"] 

interests = [ 
    ["cats", "animals", "cars"], 
    ["programming", "ssh", "stackoverflow"], 
    ["climbing", "football", "coffee"] 
] 

console.log(closest(me, interests)) 
> programming,ssh,stackoverflow