【问题标题】:Find nearest match of objects in JavaScript array在 JavaScript 数组中查找最接近的对象匹配
【发布时间】:2019-11-02 17:13:28
【问题描述】:

我有一个未排序的对象数组:

const participants = [
    {code: '222222', is_winner: true},
    {code: '444444', is_winner: false},
    {code: '777777', is_winner: false},
    {code: '555555', is_winner: true},
    {code: '666666', is_winner: false},
    {code: '111111', is_winner: false},
    {code: '333333', is_winner: false},
];

我如何找到最近的对象,is_winner 键为真,给定一个参与者,is_winner 键始终为假。

if participant = {code: '333333', is_winner: false} 
=> 
Should return {code: '555555', is_winner: true}

if participant = {code: '444444', is_winner: false} 
=> 
Should return {code: '222222', is_winner: true}

我正在寻找最接近非获胜者的获胜者(is_winner 为真)。该阵列是非圆形的。

【问题讨论】:

  • 222222 更接近 333333。你的逻辑没有意义
  • @chrispbacon 我明白你的意思。我正在寻找仅相邻的参与者,而不是再次遍历数组。在这种情况下,你是对的,222222 比 555555 更接近 333333。
  • @chrispbacon 为什么你认为数组是圆形的?我不认为这是一个公平的假设。
  • 您的问题非常不清楚。您能否对其进行编辑以反映您希望发生的情况、任何限制(如上述评论中的限制)以及预期结果?

标签: javascript arrays algorithm sorting object


【解决方案1】:

这是我从您问题中的信息中得到的答案

const participants = [
    {code: '222222', is_winner: true},
    {code: '444444', is_winner: false},
    {code: '777777', is_winner: false},
    {code: '555555', is_winner: true},
    {code: '666666', is_winner: false},
    {code: '111111', is_winner: false},
    {code: '333333', is_winner: false},
];

function findClosestWinner(code, participants) {
    let index = participants.findIndex((participant) => {
        return participant.code === code
    })
    let winner
    let awayDown = 0
    let awayUp = 0
    for (let x = index-1; x >= 0; x--) {
        let currentParticipant = participants[x]
        awayDown += 1
        if (currentParticipant.is_winner) {
            winner = currentParticipant
            break
        }
    }
    for (let x = index+1; x < participants.length; x++) {
        let currentParticipant = participants[x]
        awayUp += 1
        if (currentParticipant.is_winner && awayUp <= awayDown) {
            winner = currentParticipant
            break
        }
    }
    return winner
}
let winner = findClosestWinner('777777', participants)
console.log(winner)
findClosestWinner 获取参与者的代码和参与者列表并返回最接近的获胜参与者。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    您可以在一个循环中完成。添加了 cmets,但主体很简单,循环遍历,同时注意获胜者并将其存储,直到找到匹配的代码。找到您要搜索的代码后,即可找到上位赢家。如果找到,则找到最近的。

    在比赛中可能有一些边缘情况是获胜者,但由于您没有在示例问题中解决这些问题,我将假装它们不存在并让您为它们编程;)

    let findCloseWinner = (code, participants) => {
        var low=-1, codeIx=-1;
        for(var i=0;i<participants.length;i++) {
            if(participants[i].code === code) codeIx = i;
            if(participants[i].is_winner) {
                // if we havent found our code then we have a new lower bound
                if(codeIx === -1) {
                    low = i;
                } else {
                    // find closer, is it lower or upper
                    if(low === -1 || codeIx-low > i-codeIx) {
                        return participants[i];
                    } else {
                        return participants[low];
                    }
                }
            }
        }
        
        // If no code was found or we never found a lower bound then were done
        if(codeIx === -1 || low === -1) return null
        // no upper bound found during loop, low must be winner
        return participants[low];
    }
    
    var participants = [
        {code: '222222', is_winner: true},
        {code: '444444', is_winner: false},
        {code: '777777', is_winner: false},
        {code: '555555', is_winner: true},
        {code: '666666', is_winner: false},
        {code: '111111', is_winner: false},
        {code: '333333', is_winner: false},
    ];
    
    console.log(findCloseWinner('333333', participants));
    console.log(findCloseWinner('444444', participants));

    【讨论】:

      【解决方案3】:

      这可以很简单:

      const data = [ {code: '222222', is_winner: true}, {code: '444444', is_winner: false}, {code: '777777', is_winner: false}, {code: '555555', is_winner: true}, {code: '666666', is_winner: false}, {code: '111111', is_winner: false}, {code: '333333', is_winner: false}, ];
      
      let fn = (c, arr) => {
        let i = arr.findIndex(x => x.code === c)
        return data.slice(0, i).reverse().find(x => x.is_winner)
      }
      
      console.log(fn('333333', data))  // 555555
      console.log(fn('444444', data))  // 222222

      我们的想法是使用提供的code 获取项目的index,然后对主数组进行切片,这样您就只有该索引之前的项目。然后简单地反转结果并在is_winner 上获得第一个匹配项。

      【讨论】:

        猜你喜欢
        • 2020-01-30
        • 2011-07-24
        • 2019-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-16
        • 1970-01-01
        相关资源
        最近更新 更多