在JavaScript数组中查找对象的最接近匹配

时间:2019-06-19 22:11:43

标签: javascript arrays algorithm sorting object

我有一组未排序的对象:

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键为true,给予参与者,is_winner键始终为false。

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为true)。该阵列是非圆形的。

3 个答案:

答案 0 :(得分:1)

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

<Route render={<InstructorLoginFormComponent ... />} />
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)获取参与者的代码和参与者列表,然后返回最接近的获胜参与者。

希望这会有所帮助。

答案 1 :(得分:1)

您可以在一个循环中完成。添加了注释,但原理很简单,请在寻找获胜者并将其存储直到找到匹配的代码之前进行遍历。找到代码后,您正在寻找更高的优胜者。如果找到,则找到最接近的一个。

比赛中可能会有一些优势案例,但由于您没有在示例中解决这些问题,因此我假装它们不存在,让您为它们编程;)

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

答案 2 :(得分:1)

这可以简单地是:

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

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