JavaScript查找數(shù)組中指定元素的索引的方法小結(jié)
在 JavaScript 中,有幾種方法可以查找數(shù)組中指定元素的索引:
1. indexOf() - 查找簡(jiǎn)單值
const arr = ['apple', 'banana', 'orange', 'banana'];
// 查找第一個(gè)匹配項(xiàng)的索引
console.log(arr.indexOf('banana')); // 1
// 查找不存在的元素
console.log(arr.indexOf('grape')); // -1
// 從指定位置開始查找
console.log(arr.indexOf('banana', 2)); // 3
2. lastIndexOf() - 從后往前查找
const arr = ['apple', 'banana', 'orange', 'banana'];
console.log(arr.lastIndexOf('banana')); // 3
console.log(arr.lastIndexOf('banana', 2)); // 1
3. findIndex() - 使用回調(diào)函數(shù)查找
const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 3, name: 'Charlie' }
];
// 查找第一個(gè)滿足條件的元素索引
const index = users.findIndex(user => user.name === 'Bob');
console.log(index); // 1
// 查找年齡大于25的用戶索引
const ages = [18, 22, 30, 45];
const ageIndex = ages.findIndex(age => age > 25);
console.log(ageIndex); // 2
4. findLastIndex() - 從后往前使用回調(diào)函數(shù)查找
const numbers = [1, 2, 3, 4, 3, 5]; // 從后往前查找第一個(gè)滿足條件的元素索引 const lastIndex = numbers.findLastIndex(num => num === 3); console.log(lastIndex); // 4
5. 手動(dòng)循環(huán)實(shí)現(xiàn)
function findIndexCustom(arr, value) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === value) {
return i;
}
}
return -1;
}
const arr = ['a', 'b', 'c'];
console.log(findIndexCustom(arr, 'b')); // 1
console.log(findIndexCustom(arr, 'd')); // -1
實(shí)際應(yīng)用示例
// 查找對(duì)象數(shù)組中的特定元素
const products = [
{ id: 1, name: 'Laptop', price: 1000 },
{ id: 2, name: 'Phone', price: 500 },
{ id: 3, name: 'Tablet', price: 300 }
];
// 查找價(jià)格低于400的產(chǎn)品索引
const cheapProductIndex = products.findIndex(product => product.price < 400);
console.log(cheapProductIndex); // 2
// 查找所有匹配項(xiàng)的索引
function findAllIndexes(arr, value) {
const indexes = [];
let currentIndex = -1;
while ((currentIndex = arr.indexOf(value, currentIndex + 1)) !== -1) {
indexes.push(currentIndex);
}
return indexes;
}
const fruits = ['apple', 'banana', 'apple', 'orange', 'apple'];
console.log(findAllIndexes(fruits, 'apple')); // [0, 2, 4]
總結(jié)
- 簡(jiǎn)單值查找:使用
indexOf()或lastIndexOf() - 復(fù)雜條件查找:使用
findIndex()或findLastIndex() - 返回值:所有方法在未找到時(shí)都返回
-1 - 性能考慮:對(duì)于大型數(shù)組,
indexOf()通常比手動(dòng)循環(huán)更快
根據(jù)具體需求選擇合適的方法!
到此這篇關(guān)于JavaScript查找數(shù)組中指定元素的索引的方法小結(jié)的文章就介紹到這了,更多相關(guān)JS查找數(shù)組中指定元素的索引內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript常見數(shù)組方法之如何轉(zhuǎn)置矩陣
這篇文章主要給大家介紹了關(guān)于JavaScript常見數(shù)組方法之如何轉(zhuǎn)置矩陣的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03
js實(shí)現(xiàn)的訂閱發(fā)布者模式簡(jiǎn)單示例
這篇文章主要介紹了js實(shí)現(xiàn)的訂閱發(fā)布者模式,結(jié)合完整示例形式分析了js訂閱發(fā)布者模式相關(guān)實(shí)現(xiàn)與使用方法,需要的朋友可以參考下2020-03-03
微信小程序MUI導(dǎo)航欄透明漸變功能示例(通過改變r(jià)gba的a值實(shí)現(xiàn))
這篇文章主要介紹了微信小程序MUI導(dǎo)航欄透明漸變功能,結(jié)合實(shí)例形式分析了通過改變r(jià)gba的a值實(shí)現(xiàn)透明度漸變功能的相關(guān)操作技巧,需要的朋友可以參考下2019-01-01
js實(shí)現(xiàn)簡(jiǎn)易聊天對(duì)話框
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)簡(jiǎn)易聊天對(duì)話框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
前端實(shí)現(xiàn)代碼質(zhì)量校驗(yàn)的常用方法與避坑指南
這篇文章主要為大家詳細(xì)介紹了前端HTML5代碼的質(zhì)量校驗(yàn)方法,文中的示例代碼講解詳細(xì),希望可以幫助大家規(guī)劃HTML5+AI的學(xué)習(xí)與職業(yè)路徑2026-04-04
JavaScript剩余操作符Rest Operator詳解
在本篇文章里小編給各位分享的是關(guān)于JavaScript剩余操作符Rest Operator知識(shí)點(diǎn)用法總結(jié),有需要的朋友們跟著學(xué)習(xí)下。2019-07-07
利用JS實(shí)現(xiàn)簡(jiǎn)單的日期選擇插件
這篇文章主要介紹了利用JS實(shí)現(xiàn)簡(jiǎn)單的日期選擇插件,文中實(shí)現(xiàn)兩種效果的日期選擇,一種是默認(rèn)參數(shù),點(diǎn)擊日期后直接選擇該日期,另一種是顯示按鈕來設(shè)置時(shí)間,有需要的朋友可以參考借鑒,下面來一起看看吧。2017-01-01

