Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript查找2数组中对象的索引_Javascript_Arrays_Indexof - Fatal编程技术网

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

javascript查找2数组中对象的索引,javascript,arrays,indexof,Javascript,Arrays,Indexof,我有两个数组 第一个数组有firstname和lastname 第二个数组只有firstname 我将索引一个firstname并检查数组 许可的功能(人){ var指数=1; 对于(变量i=0;i{ for(设i=0;i{ for(设i=0;i

我有两个数组

第一个数组有
firstname
lastname
第二个数组只有
firstname

我将索引一个firstname并检查数组

许可的功能(人){
var指数=1;
对于(变量i=0;iconsole.log(whereIsAlice(friends2));//应该是-1
如果您想编写自己的循环,可以这样做

var-friends1=[
{名字:“约翰”,姓氏:“高德”},
{姓:“丽莎”,姓:“麦克拉纳汉”},
{姓氏:“爱丽丝”,姓氏:“沃勒”},
{姓氏:'Marine',姓氏:'Salsbury'},
];
变量2=[
{firstName:'Tim'},
{名字:'亚瑟'},
{名字:'胡安'},
];
常量whereIsAlice=arr=>{
for(设i=0;iconsole.log(whereIsAlice(friends2));//应该是-1
如果您想编写自己的循环,可以这样做

var-friends1=[
{名字:“约翰”,姓氏:“高德”},
{姓:“丽莎”,姓:“麦克拉纳汉”},
{姓氏:“爱丽丝”,姓氏:“沃勒”},
{姓氏:'Marine',姓氏:'Salsbury'},
];
变量2=[
{firstName:'Tim'},
{名字:'亚瑟'},
{名字:'胡安'},
];
常量whereIsAlice=arr=>{
for(设i=0;iconsole.log(whereIsAlice(friends2));//应该是-1
这个问题很简单。您将在其中比较isAlice方法始终为第一个数组和第二个数组,然后该方法始终为循环找到值并断开de

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
函数whereIsAlice(名称){
对于(var i=0;i
这个问题很容易解决。您将在其中比较isAlice方法始终为第一个数组和第二个数组,然后该方法始终为循环找到值并断开de

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
函数whereIsAlice(名称){
对于(var i=0;i
要简化功能,可以使用下一个代码

许可的功能(人){
returnpersons.map(函数(e){returne e.firstName;}).indexOf('Alice');
}
变量1=[{
名字:“约翰”,
姓氏:'Gaudet'
},
{
名字:“丽莎”,
姓:“麦克拉纳汉”
},
{
名字:“爱丽丝”,
姓氏:“Vore”
},//爱丽丝在这里,在索引2处
{
名字:“海军陆战队”,
姓氏:“萨尔斯伯里”
},
];
变量2=[{
名字:“蒂姆”
},
{
名字:“亚瑟”
},
{
名字:“胡安”
},
];
console.log(whereIsAlice(friends1))//应该是2

console.log(whereIsAlice(friends2));//应该是-1
为了简化您的函数,您可以使用下一个代码

许可的功能(人){
returnpersons.map(函数(e){returne e.firstName;}).indexOf('Alice');
}
变量1=[{
名字:“约翰”,
姓氏:'Gaudet'
},
{
名字:“丽莎”,
姓:“麦克拉纳汉”
},
{
名字:“爱丽丝”,
姓氏:“Vore”
},//爱丽丝在这里,在索引2处
{
名字:“海军陆战队”,
姓氏:“萨尔斯伯里”
},
];
变量2=[{
名字:“蒂姆”
},
{
名字:“亚瑟”
},
{
名字:“胡安”
},
];
console.log(whereIsAlice(friends1))//应该是2

console.log(whereIsAlice(friends2));//应为-1
按如下方式重写函数,这将在您的情况下正常工作:

function whereIsAlice(persons) {
    var index= -1;
    for(var i=0;i<persons.length;i++)
        if(persons[i].firstName==="Alice"){index=i;break;}
    return index;
}
许可的功能(人){
var指数=-1;

对于(var i=0;i按如下方式重写函数,这将在您的情况下正常工作:

function whereIsAlice(persons) {
    var index= -1;
    for(var i=0;i<persons.length;i++)
        if(persons[i].firstName==="Alice"){index=i;break;}
    return index;
}
许可的功能(人){
var指数=-1;

对于(var i=0;i你们中的一些人从初始数组中变异新数组(
array.map
),并执行
indexOf

其他人正在循环使用
for
,然后检查