简体   繁体   English

javascript在2数组中查找对象的索引

[英]javascript find index of object in 2 array

I have 2 arrays 我有2个数组

First array has firstname and lastname Second array only has firstname 第一个数组具有firstnamelastname第二个数组仅具有firstname

I will index one firstname and check the arrays 我将索引一个名字并检查数组

 function whereIsAlice(persons) { var index = 1; for (var i = 0; i < friends1.length; i++) { if (friends1[i].firstName === "Alice") { index = i; break; } if (friends2[i].firstName === "Alice") { index = i; } } return index } var friends1 = [{ firstName: 'John', lastName: 'Gaudet' }, { firstName: 'Lisa', lastName: 'Mcclanahan' }, { firstName: 'Alice', lastName: 'Vore' }, // Alice is here, at index 2 { firstName: 'Marine', lastName: 'Salsbury' }, ]; var friends2 = [{ firstName: 'Tim' }, { firstName: 'Arthur' }, { firstName: 'Juan' }, ]; console.log(whereIsAlice(friends1)); //Should be 2 console.log(whereIsAlice(friends2)); // Should be -1 

The output are 2 on both. 两者的输出均为2。 How can i fix it ? 我该如何解决?

You can do it like this if you want to write your own loop. 如果要编写自己的循环,可以这样做。

 var friends1 = [ { firstName: 'John', lastName: 'Gaudet' }, { firstName: 'Lisa', lastName: 'Mcclanahan' }, { firstName: 'Alice', lastName: 'Vore' }, { firstName: 'Marine', lastName: 'Salsbury' }, ]; var friends2 = [ { firstName: 'Tim' }, { firstName: 'Arthur' }, { firstName: 'Juan' }, ]; const whereIsAlice = arr => { for (let i = 0; i < arr.length; i++) { if (arr[i].firstName === 'Alice') { return i; } } return -1; } console.log(whereIsAlice(friends1)); //Should be 2 console.log(whereIsAlice(friends2)); // Should be -1 

The problem its easy. 这个问题很容易。 You are comparing in whereIsAlice method always the first array and the second array, then the method always find the value and break de for loop. 您在whereIsAlice方法中始终比较第一个数组和第二个数组,然后该方法始终查找值并中断for循环。

function whereIsAlice(names) {
    for (var i = 0; i < names.length; i++) {
        if (names[i].firstName == "Alice") {
            return i;
        } 
    }

    // When not found in above loop then return -1, not found!
    return -1;
}


var friends1 = [
    { firstName: 'John', lastName: 'Gaudet' },
    { firstName: 'Lisa', lastName: 'Mcclanahan' },
    { firstName: 'Alice', lastName: 'Vore' }, // Alice is here, at index 2
    { firstName: 'Marine', lastName: 'Salsbury' }
];
var friends2 = [
    { firstName: 'Tim' },
    { firstName: 'Arthur' },
    { firstName: 'Juan' }
];
console.log(whereIsAlice(friends1)); //Should be 2
console.log(whereIsAlice(friends2)); // Should be -1

to simplify your Function you can use the next code 为了简化您的功能,您可以使用下一个代码

 function whereIsAlice(persons) { return persons.map(function(e) { return e.firstName; }).indexOf('Alice'); } var friends1 = [{ firstName: 'John', lastName: 'Gaudet' }, { firstName: 'Lisa', lastName: 'Mcclanahan' }, { firstName: 'Alice', lastName: 'Vore' }, // Alice is here, at index 2 { firstName: 'Marine', lastName: 'Salsbury' }, ]; var friends2 = [{ firstName: 'Tim' }, { firstName: 'Arthur' }, { firstName: 'Juan' }, ]; console.log(whereIsAlice(friends1)); //Should be 2 console.log(whereIsAlice(friends2)); // Should be -1 

Rewrite Your function as below, This will work properly in your case : 如下重写您的函数,这将在您的情况下正常工作:

function whereIsAlice(persons) {
    var index= -1;
    for(var i=0;i<persons.length;i++)
        if(persons[i].firstName==="Alice"){index=i;break;}
    return index;
}

Some of You mutating new array ( array.map ) from initial array and doing indexOf . 你们中的一些人从初始数组中变异出新数组( array.map )并执行indexOf

Others are looping with for and then checking the variable index . 其他人正在与for循环,然后检查变量index

Then why JS community is working to extend language constructions, methods and etc? 那么,为什么JS社区正在努力扩展语言构造,方法等?

I recommend You to dive in MDN better and read about findIndex ? 我建议您更好地参加MDN并了解findIndex

 function whereIsAlice(persons) { return persons.findIndex(function(person) { return person.firstName === 'Alice'; }); } var friends1 = [ { firstName: 'John', lastName: 'Gaudet' }, { firstName: 'Lisa', lastName: 'Mcclanahan' }, { firstName: 'Alice', lastName: 'Vore' }, // Alice is here, at index 2 { firstName: 'Marine', lastName: 'Salsbury' }, ]; var friends2 = [ { firstName: 'Tim' }, { firstName: 'Arthur' }, { firstName: 'Juan' }, ]; console.log(whereIsAlice(friends1)); console.log(whereIsAlice(friends2)); 

With ES6 it's so shorter that I don't see reason to create method: 使用ES6,它是如此之短,以至于我看不到创建方法的理由:

console.log(friends1.findIndex(friend => friend.firstName === 'Alice'));
console.log(friends2.findIndex(friend => friend.firstName === 'Alice'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM