找到几个数组之间最接近的匹配

时间:2014-01-10 09:12:32

标签: javascript arrays object

我正在创建一个聊天脚本,用户可以在其中设置自己的兴趣。当用户连接到服务器时,他们的客户端通过WebSocket发送以下JSON:

{"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"]}

如果找不到任何兴趣相似的条目,则应将该用户添加到等待列表数组中。

我非常坚持这个,所以有人可以帮忙吗?


不确定为什么这个问题被低估了。小心添加评论?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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);
}