通过自定义函数查找元素下标,元素数据结构不一样就需要写多次
function findIndex(list, item) { for (let i in list) { if (list[i] == item) { return i; } } // 没有找到元素返回-1 return -1; } const list = ["a", "b", "c"]; let item = "b"; let index = findIndex(list, item); console.log(index); // 1
使用findIndex查找列表元素下标
const list = ["a", "b", "c"]; let index = list.findIndex((x) => x == "b"); console.log(index); // 1