Search ng Array ng JavaScript

数组查找和搜索方法

另请参阅:

JavaScript Array indexOf()

indexOf() 方法在数组中搜索元素值并返回其位置。

注意:第一项的位置是 0,第二项的位置是 1,依此类推。

Example

在数组中搜索项 "Apple":

const fruits = ["Apple", "Orange", "Apple", "Mango"];
let position = fruits.indexOf("Apple") + 1;

Try It Yourself

语法

array.indexOf(item, start)
item 必需。要搜索的项。
start 可选。搜索的起始位置。负值将从数组末尾开始计数,并搜索到末尾。

返回值:

  • 如果未找到项,则返回 -1。
  • 如果项出现多次,则返回第一次出现的位置。

JavaScript Array lastIndexOf()

lastIndexOf() 方法与 indexOf() 相同,但返回指定元素最后一次出现的位置。

Example

在数组中搜索项 "Apple":

const fruits = ["Apple", "Orange", "Apple", "Mango"];
let position = fruits.lastIndexOf("Apple") + 1;

Try It Yourself

语法

array.lastIndexOf(item, start)
item 必需。要搜索的项。
start 可选。搜索的起始位置。负值将从数组末尾开始计数,并搜索到开头。

JavaScript Array includes()

ECMAScript 2016 引入了 Array.includes() 方法。它允许我们检查数组中是否包含某个元素(包括 NaN,与 indexOf 不同)。

Example

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.includes("Mango"); // 返回 true

Try It Yourself

语法

array.includes(search-item)

search-item:要搜索的元素。

注意:includes() 可以检查 NaN 值,而 indexOf() 不能。

Browser Support

includes()ECMAScript 2016 的特性。

自 2017 年 3 月起,所有现代浏览器均支持 ES2016:

Chrome Edge Firefox Safari Opera
Chrome 52 Edge 15 Firefox 52 Safari 10.1 Opera 39
2016 年 7 月 2017 年 4 月 2017 年 3 月 2017 年 5 月 2016 年 8 月

Internet Explorer 不支持 includes()。

JavaScript Array find()

find() 方法返回通过测试函数的第一个数组元素的值。

Example

查找第一个大于 18 的元素:

const numbers = [4, 9, 16, 25, 29];
let first = numbers.find(myFunction);
function myFunction(value, index, array) {
  return value > 18;
}

Try It Yourself

注意:函数接受 3 个参数:

  • 元素值
  • 元素索引
  • 数组本身

Browser Support

find() 是 ES6(JavaScript 2015)的特性。

自 2017 年 6 月起,所有现代浏览器均支持 ES6:

Chrome Edge Firefox Safari Opera
Chrome 51 Edge 15 Firefox 54 Safari 10 Opera 38
2016 年 5 月 2017 年 4 月 2017 年 6 月 2016 年 9 月 2016 年 6 月

Internet Explorer 不支持 find().

JavaScript Array findIndex()

findIndex() 方法返回通过测试函数的第一个数组元素的索引。

Example

查找第一个大于 18 的元素的索引:

const numbers = [4, 9, 16, 25, 29];
let first = numbers.findIndex(myFunction);
function myFunction(value, index, array) {
  return value > 18;
}

Try It Yourself

注意:函数接受 3 个参数:

  • 元素值
  • 元素索引
  • 数组本身

Browser Support

findIndex() 是 ES6(JavaScript 2015)的特性。

自 2017 年 6 月起,所有现代浏览器均支持 ES6:

Chrome Edge Firefox Safari Opera
Chrome 51 Edge 15 Firefox 54 Safari 10 Opera 38
2016 年 5 月 2017 年 4 月 2017 年 6 月 2016 年 9 月 2016 年 6 月

Internet Explorer 不支持 findIndex().

JavaScript Array findLast()

ES2023 新增了 findLast() 方法,它从数组末尾开始搜索,并返回满足条件的第一个元素的值。

Example

const temp = [27, 28, 30, 40, 42, 35, 30];
let mataas = temp.findLast(x => x > 40);

Try It Yourself

Browser Support

findLast() Is an ES2023 feature.

Starting from July 2023, all modern browsers support:

Chrome Edge Firefox Safari Opera
Chrome 110 Edge 110 Firefox 115 Safari 16.4 Opera 96
February 2023 February 2023 July 2023 March 2023 May 2023

JavaScript Array findLastIndex() Method

findLastIndex() Method finds the index of the last element that meets the condition.

Example

const temp = [27, 28, 30, 40, 42, 35, 30];
let pos = temp.findLastIndex(x => x > 40);

Try It Yourself

Browser Support

findLastIndex() Is an ES2023 feature.

Starting from July 2023, all modern browsers support:

Chrome Edge Firefox Safari Opera
Chrome 110 Edge 110 Firefox 115 Safari 16.4 Opera 96
February 2023 February 2023 July 2023 March 2023 May 2023

Complete Array Reference Manual

For a complete array reference, please visit:JavaScript Array Reference Manual.

This manual includes descriptions and examples of all array properties and methods.