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

javascript find index of object in 2 array

我有 2 个数组

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

我将索引一个名字并检查数组

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

两者的输出都是2。我该如何解决?

如果您想编写自己的循环,可以这样做。

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

这个问题很简单。您在 whereIsAlice 方法中总是比较第一个数组和第二个数组,然后该方法总是找到值并中断 de 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

要简化您的函数,您可以使用下一个代码

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

如下重写您的函数,这将在您的情况下正常工作:

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

你们中的一些人从初始数组改变新数组 (array.map) 并执行 indexOf.

其他人用for循环,然后检查变量index

那为什么 JS 社区致力于扩展语言结构、方法等?

我建议您更好地研究 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));

对于 ES6,它是如此的短以至于我看不出创建方法的理由:

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