码迷,mamicode.com
首页 > Web开发 > 详细

关于js查找和筛选的几种方式

时间:2019-11-29 11:17:40      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:字符   indexof   数组   object   元素   lte   表达   检测   gobject   

find();

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

find() 方法为数组中的每个元素都调用一次函数执行:

  • 当数组中的元素在测试条件时返回 true 时, find() 返回符合条件的元素,之后的值不会再调用执行函数。
  • 如果没有符合条件的元素返回 undefined

注意: find() 对于空数组,函数是不会执行的。

注意: find() 并没有改变数组的原始值。

[1,2,3,4,5,6].find((n) => n < 5)
//找出数组中第一个大于5 的成员
// 6
array.find(function(currentValue, index, arr),thisValue)
currentValue : 必需。当前元素
index:可选。当前元素的索引值
arr: 可选。当前元素所属的数组对象
thisValue: 可选。 传递给函数的值一般用 "this" 值。
如果这个参数为空, "undefined" 会传递给 "this" 值

findIndex();

findIndex() 方法返回传入一个测试条件(函数)符合条件的数组第一个元素位置。

findIndex() 方法为数组中的每个元素都调用一次函数执行:

  • 当数组中的元素在测试条件时返回 true 时, findIndex() 返回符合条件的元素的索引位置,之后的值不会再调用执行函数。
  • 如果没有符合条件的元素返回 -1

注意: findIndex() 对于空数组,函数是不会执行的。

注意: findIndex() 并没有改变数组的原始值。

[3,10,18,19].findIndex((n) => n >= 18)
//返回符合条件的值的位置(索引)
// 2
array.findIndex(function(currentValue, index, arr),thisValue)
currentValue : 必需。当前元素
index:可选。当前元素的索引值
arr: 可选。当前元素所属的数组对象
thisValue: 可选。 传递给函数的值一般用 "this" 值。
如果这个参数为空, "undefined" 会传递给 "this" 值

filter();

filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。(返回数组,包含了符合条件的所有元素。如果没有符合条件的元素则返回空数组。)

注意: filter() 不会改变原始数组。

注意: filter() 不会对空数组进行检测。

var arr = [1,2,3,4,5,6,7]
var newArr = arr.filter(item => item > 5);
console.log(newArr); //[6, 7]
array.filter(function(currentValue, index, arr),thisValue)
currentValue : 必需。当前元素
index:可选。当前元素的索引值
arr: 可选。当前元素所属的数组对象
thisValue: 可选。 传递给函数的值一般用 "this" 值。
如果这个参数为空, "undefined" 会传递给 "this" 值
//数组去重
var arr = [1,2,2,3,4,4,5,6,6,7,8,8,9];
var newArr = arr.filter((x, index,self)=>self.indexOf(x) === index)  
console.log(newArr); // [1, 2, 3, 4, 5, 6, 7, 8, 9]

indexOf();

indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。

注释:indexOf() 方法对大小写敏感!

注释:如果要检索的字符串值没有出现,则该方法返回 -1。

lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索。

stringObject.indexOf(searchvalue,fromindex)
该方法将从头到尾地检索字符串 stringObject,看它是否含有子串 
searchvalue。开始检索的位置在字符串的 fromindex 处或字符串的开头(没
有指定 fromindex 时)。如果找到一个 searchvalue,则返回 searchvalue
的第一次出现的位置。stringObject 中的字符位置是从 0 开始的。

 

var str= "aaa456ac";
console.log(arr.indexOf(‘b‘)); // -1 ,  字符b第一次出现的位置,没有,返回-1;
console.log(arr.indexOf(‘a‘)); // 0 ,  字符a第一次出现的位置,是 0
console.log(arr.indexOf(‘a‘, 3)); //  6,   从第四个字符位置开始往后继续查找,包含当前位置  
console.log(arr.indexOf(‘ac‘, 3)); //  6,   字符串ac第一次出现的位置
console.log(arr.lastIndexOf(‘a‘)); //  6,   字符串a最后出现的位置
 

some() ;

some() 方法用于检测数组中的元素是否满足指定条件(函数提供)。

some() 方法会依次执行数组的每个元素:

  • 如果有一个元素满足条件,则表达式返回true , 剩余的元素不会再执行检测。
  • 如果没有满足条件的元素,则返回false。

注意: some() 不会对空数组进行检测。

注意: some() 不会改变原始数组。

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

var arr = [1,2,3,4,5,6,7]
var isHas = arr.some(item => item > 5);
console.log(isHas ); //  true
var isHas2 = arr.some(item => item > 7);
console.log(isHas2 ); //  false

every() ;

every() 方法用于检测数组所有元素是否都符合指定条件(通过函数提供)。

every() 方法使用指定函数检测数组中的所有元素:

  • 如果数组中检测到有一个元素不满足,则整个表达式返回 false ,且剩余的元素不会再进行检测。
  • 如果所有元素都满足条件,则返回 true。

注意: every() 不会对空数组进行检测。

注意: every() 不会改变原始数组。

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

var arr = [1,2,3,4,5,6,7]
var isHas = arr.every(item => item > 5);
console.log(isHas ); //  false
var isHas2 = arr.every(item => item < 8);
console.log(isHas2 ); //  true

 

关于js查找和筛选的几种方式

标签:字符   indexof   数组   object   元素   lte   表达   检测   gobject   

原文地址:https://www.cnblogs.com/lvsk/p/11955937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!