8、数组检测、查找
返回数组中满足传入函数条件的第一个元素的值。否则返回 undefined。find方法不会改变原数组。
find方法对数组中的每一项元素执行一次 callback 函数,直至有一个 callback 返回 true。当找到了这样一个元素后,该方法会立即返回这个元素的值,否则返回 undefined。
注意 callback 函数会为数组中的每个索引调用即从 0 到 length - 1,而不仅仅是那些被赋值的索引。(这点是区别于map和fliter等等方法的)
返回数组中符合条件的第一个值,在数组中找到的话则不会往下执行。 如果数组中没有符合条件的值则返回undefined
在find方法执行之后,给数组新增值是访问不到的。 在callback还未访问到某个值是如果改变某一个值那么当callback访问到他时,将是新的值。 如果在find方法执行后删除某个值还是会访问到他。
注意: find() 对于空数组,函数是不会执行的。find() 并没有改变数组的原始值。
语法:array.find(function(currentValue, index, arr),thisValue
参数:currentValue:必需。当前元素
Index: 可选。当前元素的索引值
arr: 可选。当前元素所属的数组对象
thisValue: 可选。 传递给函数的值一般用 “this” 值。如果这个参数为空, “undefined” 会传递给 “this” 值
find方法返回值:数组中第一个满足所传入callback函数的元素的值,否则返回 undefined。
会访问到他。
var array = [1, 2, 3, 4, 5, 6, 6, 6]
var result = array.find(function (item, index, arr) {
console.log(this); // [1, 2, 3, 4, 5, 6, 6, 6]
return item > 6;
}, array)
console.log(result); // undefined
console.log(array); //[1, 2, 3, 4, 5, 6, 6, 6]
var result = array.find(function (item, index, arr) {
console.log(this); // [1, 2, 3, 4, 5, 6, 6, 6]
return item > 5;
}, array)
console.log(result); // 6
console.log(array); //[1, 2, 3, 4, 5, 6, 6, 6]
// 定义一个需要被查找的数组
var memoList = [{id: 1, name: '1'},{id: 2, name: '2'},{id: 3, name: '3'}]
// 用 editItem 变量将 查找出来的数据进行接收
var editItem = memoList.find((ele) => {
return ele.id == 2
}) // 打印 editItem 得到的结果是 {id: 2, name: '2'}
findIndex() 方法返回传入一个测试条件(函数)符合条件的数组第一个元素位置。
一旦找到满足测试函数的元素,它就不会执行该函数。
它不会更改原始数组。
语法:array.findIndex(function(currentValue, index, arr), thisValue);
参数:currentValue: 必需。当前元素
Index: 可选。当前元素的索引值
arr: 可选。当前元素所属的数组对象
thisValue: 可选。 传递给函数的值一般用 “this” 值。如果这个参数为空, “undefined” 会传递给 “this” 值
用法:findIndex() 方法为数组中的每个元素都调用一次函数执行:当数组中的元素在测试条件时返回 true 时, findIndex() 返回符合条件的元素的索引位置,之后的值不会再调用执行函数;如果没有符合条件的元素返回 -1。
var array = [ 10, 20, 30, 110, 60 ];
function finding_index(element) { return element > 25; }
console.log(array.findIndex(finding_index)); // 2
Array.indexOf 检测当前项在数组中第一次出现的位置。
//数组调用
该方法将从头到尾地检索数组,看它是否含有对应的元素。开始检索的位置在数组 start 处或数组的开头(没有指定 start参数时)。如果找到一个 item,则返回 item 的第一次出现的位置。
如果在数组中没找到指定元素则返回 -1。
语法:array.indexOf(item,start)
参数:item 必须 要查找的元素的位置,
start 非必须可选的整数参数。规定在数组中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的首字符开始检索。
let food= ["番茄", "胡萝卜", "排骨", "苹果"];
let a = food.indexOf("苹果");
console.log(a) // 3
let b= food.indexOf("香蕉");
console.log(b) // -1
//字符串调用
indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。区分大小写
如果要检索的字符串值没有出现,则该方法返回 -1。
语法:string.indexOf(value,start)
参数:value 必须 要查找的元素的位置
start 可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是 0 到 string.length - 1。如省略该参数,则将从字符串的首字符开始检索。
let str="Hello world!";
console.log(str.indexOf("Hello")); //0
console.log(str.indexOf("World") ); //-1
console.log(str.indexOf("world")); //6
array.lastindexOf 检测当前项在数组中最后一次出现的位置。
indexOf和lastIndexOf语法是一样的,但查找的顺序相反,indexOf是从前往后查,而lastIndexOf是从后往前查。
返回值:当前项出现的位置的索引(数字),如果数组中没有这一项,返回结果为-1。IE6、7、8不兼容此方法。
var ary=[1,2,3,4,5]
res=ary.indexOf("2")
console.log(res); // 1(索引值)
Array.isArray(参数)判断参数是不是数组,返回布尔值;
语法:Array.isArray(参数)
参数:必填,判断数组的值。判断传入的对象或值是否是一个数组
var arr = [1,2,3];
var num = 123;
console.log(Array.isArray(arr)); //true
console.log(Array.isArray(num)); //false
array.includes() 方法用来判断一个数组是否包含一个指定的值,如果是则返回true,否则false.
语法: arr.includes(searchElement)
arr.includes(searchElement, fromIndex)
参数:
searchElement 必须参数。需要查找的元素值
fromIndex 可选参数。从该索引处开始查找searchElement。如果为负值,则按升序从array.length + fromIndex 的索引开始搜索,默认为0.
针对于fromIndex有两种情况:
正常情况:
var ary=[1,2,3,4,5]
res=ary.includes(2)
console.log(res); // true
第一种情况:如果fromIndex 大于等于数组长度 ,则返回 false 。该数组不会被搜索:
var arr = ['a', 'b', 'c'];
arr.includes('c', 3); //false
arr.includes('c', 100); // false
第二种情况:如果 fromIndex 为负值,计算出的索引将作为开始搜索searchElement的位置。如果计算出的索引小于 0,则整个数组都会被搜索。
计算出的索引小于 0:
// 数组长度是3。// fromIndex 是 -100。 // computed index 是 3 + (-100) = -97
var arr = ['a', 'b', 'c'];
arr.includes('a', -100); // true
arr.includes('b', -100); // true
arr.includes('c', -100); // true
计算出的索引不小于0:
长度为3,-1+3=2 则升序查询下标0,1,2 下标为2的数为4,所以为true
[1, 2, 4].includes(4, -1); // true
数组
includes 可以判断一个数组中是否包含某一个元素,并返回true 或者false
['a','b','c'].includes('a') //true
['a','b','c'].includes(1) //false
includes可以包含两个参数,第二个参数表示判断的起始位置,起始位置第一个数字是0。
字符串
查找字符串是否包含 "Runoob":
var str = "Hello world, welcome to the Runoob。";
var n = str.includes("Runoob"); //true
本人是以网络视频与网络文章的方式自学的,并按自己理解的方式总结了学习笔记。有不正确之处请各位高手多多指点,一起学习进步。【VX:czlingyun 暗号:CSDN】