JS中数组查询的方法indexOf()、lastIndexOf()、includes()、find()、findIndex()、filter()、every()

JS中涉及到数组查询的方法见下表:

方法描述参数返回值
indexOf()搜索数组中的元素,并返回它所在的位置。要搜索的元素 ,查找的起始位置元素第一次出现的索引
lastIndexOf()搜索数组中的元素,并返回它最后出现的位置要搜索的元素 ,查找的起始位置元素最后一次出现的索引
includes()判断一个数组是否包含一个指定的值要查找的元素值Boolean
find()返回符合传入测试(函数)条件的数组元素函数,this符合条件的第一个元素的
findIndex()返回符合传入测试(函数)条件的数组元素索引函数,this符合条件的第一个元素的索引
every()检测数值元素的每个元素是否都符合条件函数,this元素的
some()检测数组元素中是否有元素符合指定条件函数,this元素的
filter()检测数值元素,并返回符合条件所有元素的数组函数,this数组

这些都是数组的实例方法

indexOf() 方法

搜索数组中的元素,并返回它所在的位置。在数组中从前往后查找一个元素,并返回索引,找不到则返回-1

array.indexOf(item,start)

参数:
① item 要查找的元素 ,必须
② start 开始查找的起始位置,可选,默认从位置0开始查找

var arr= [1,2,3,1,2];
console.log(arr.indexOf(2));      //1,从前到后找到的第一个2的索引是1
console.log(arr.indexOf(2,4));    //4,从第5个元素开始查收元素2,找到的元素的索引是4
console.log(arr.indexOf(4))       //-1,没有到元素4

lastIndexOf() 方法

搜索数组中的元素,并返回它最后出现的位置。在数组中从后向前查找一个元素,并返回索引,找不到则返回-1

array.lastIndexOf(item,start)

参数:
① item 要查找的元素 ,必须
② start 开始查找的起始位置,可选,默认将从字符串的最后一个元素开始检索

var arr= [1,2,3,1,2];
console.log(arr.lastIndexOf(2))   //4,从后到前找到的第一个2的索引是4

includes()方法

判断一个数组是否包含一个指定的值,返回 true 或 false

arr.includes(searchElement, fromIndex)

参数:
① searchElement 必须。需要查找的元素值。
② fromIndex 可选。开始查找的起始位置,默认为 0。

var arr1=[1,2,3,4,5];
console.log(arr1.includes(3));  //true

find()方法

返回数组中第一个满足条件的元素,测试条件是find()的参数,如果没有满足要求的元素,则返回undefined。

array.find(function(currentValue, index, arr),thisValue)

参数:
① function(currentValue, index,arr) 查询条件,必须,数组每个元素需要执行这个函数

  • currentValue 必需。当前元素
  • index 可选。当前元素的索引值
  • arr 可选。当前元素所属的数组对象

② thisValue 可选。 传递给函数的值一般用 “this” 值。默认为 “this” 值

var arr1=[1,2,3,4,5];
var res=arr1.find(function (value,index,arr1) {
  return value>3;
});
console.log(res);  //4

findIndex()方法

返回数组中第一个满足要求的元素的索引,参数同样是一个函数,如果没有满足要求的元素,则返回-1。

array.findIndex(function(currentValue, index, arr), thisValue)

参数:
① function(currentValue, index,arr) 查询条件,必须,数组每个元素需要执行这个函数

  • currentValue 必需。当前元素
  • index 可选。当前元素的索引值
  • arr 可选。当前元素所属的数组对象

② thisValue 可选。 传递给函数的值一般用 “this” 值。默认为 “this” 值

var arr1=[1,2,3,4,5];
var res=arr1.findIndex(function (value,index,arr1) {
  return value>3;
});
console.log(res);  //3

filter()方法

遍历数组中的每一项,返回过滤后的结果数组,用来创建一个新数组,对原数组没有影响。

array.filter(function(currentValue,index,arr), thisValue)

参数:
① function(currentValue, index,arr) 过滤条件,必须,数组每个元素需要执行这个函数

  • currentValue 必需。当前元素
  • index 可选。当前元素的索引值
  • arr 可选。当前元素所属的数组对象

② thisValue 可选。 传递给函数的值一般用 “this” 值。默认为 “this” 值

var arr1=[1,2,3,4,5];
var arr2=arr1.filter(function (value,index,arr1) {
  return value>3;
});
console.log(arr1);  //[1, 2, 3, 4, 5]
console.log(arr2);  //[4, 5]

every()方法

遍历数组中的每一项,检测数值元素的每个元素是否都符合条件,结果全部为true,返回true,否则返回false

array.every(function(currentValue,index,arr), thisValue)

参数:
① function(currentValue, index,arr) 检测条件,必须,数组每个元素需要执行这个函数

  • currentValue 必需。当前元素
  • index 可选。当前元素的索引值
  • arr 可选。当前元素所属的数组对象

② thisValue 可选。 传递给函数的值一般用 “this” 值。默认为 “this” 值

var arr1=[1,2,3,4,5];
var result=arr1.every(function (value,index,arr1) {
  return value>3;
});
console.log(result);  //false

some()方法

对数组中的元素进行遍历,检测数组元素中是否有元素符合指定条件,只要有结果为true,返回true,全部为false,返回false

array.some(function(currentValue,index,arr),thisValue)

参数:
① function(currentValue, index,arr) 检测条件,必须,数组每个元素需要执行这个函数

  • currentValue 必需。当前元素
  • index 可选。当前元素的索引值
  • arr 可选。当前元素所属的数组对象

② thisValue 可选。 传递给函数的值一般用 “this” 值。默认为 “this” 值

var arr1=[1,2,3,4,5];
var res=arr1.some(function (value,index,arr1) {
  return value>3;
});
console.log(res);  //true
  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在JavaScript数组的find方法用于查找数组中满足指定条件的第一个元素。它接受一个回调函数作为参数,该回调函数可以接受三个参数:元素值、元素索引和原数组。回调函数应该返回一个布尔值,表示是否满足条件。如果找到满足条件的元素,则返回该元素;如果没有找到,则返回undefined。\[1\] 例如,我们有一个数组arr = \[1, 2, 3, 4, 5, 622, 21\],我们可以使用find方法来查找大于5的第一个元素,代码如下: ``` var arr = \[1, 2, 3, 4, 5, 622, 21\]; var result = arr.find(function(age){ return age > 5; }); console.log(result); // 输出 622 ``` 如果没有找到满足条件的元素,find方法将返回undefined。例如,我们查找小于0的元素: ``` var arr = \[1, 2, 3, 4, 5, 622, 21\]; var result = arr.find(function(age){ return age < 0; }); console.log(result); // 输出 undefined ``` 需要注意的是,find方法只会返回满足条件的第一个元素,如果数组中有多个元素满足条件,它也只会返回第一个满足条件的元素。如果需要找到所有满足条件的元素,可以使用filter方法。\[2\] #### 引用[.reference_title] - *1* [JavaScript数组方法之 —— fill()、find()、findIndex()、indexOf()、lastIndexOf()](https://blog.csdn.net/z1324402468/article/details/94746734)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [JavaScript 数组方法find()](https://blog.csdn.net/weixin_46585382/article/details/125331892)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [js数组的5种查询方式——find(),findIndex(),indexOf(),lastIndexOf(),include()](https://blog.csdn.net/weixin_42062766/article/details/119133489)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值