您的位置:首页 > 其它

数组的查询

2018-04-02 16:44 120 查看
数组常用的查询方法

Array.prototype.slice()

- 作用: 截取数组

- 参数:
slice(n[, m])


- 返回值: 把找到的内容当做一个新的数组返回

- 原有数组是否改变: 否

let ary = [1,2,3,4,5,6,7]


slice(n)
从索引
n
找到数组末尾 如果
m
被省略 则slice会截取到数组末尾

let returnValue = ary.slice(2)

console.log(ary) //[1, 2, 3, 4, 5, 6, 7]
console.log(returnValue) //[3, 4, 5, 6, 7]


slice(n,m)
从索引
n
找到索引
m
处(不包含
m
这一项)

let returnValue = ary.slice(2,6)
let returnValue1 = ary.slice(-4,-1)
console.log(ary) //[1, 2, 3, 4, 5, 6, 7]
console.log(returnValue) //[3, 4, 5, 6]
console.log(returnValue1) //[4, 5, 6]


Array.prototype.indexOf()

- 作用: 查询数组里面是否包含某个元素

- 参数:
indexOf(value[,index])


- 返回值: 如果查询的元素存在 返回当前元素的索引; 如果查询的元素不存在 返回 -1

- 原有数组是否改变: 否

indexOf(value[,index])
查询数组里面是否包含某个元素
index
查询开始的索引 默认为0

let ary = [1,2,3,'a','b','c',NaN]
let returnValue = ary.indexOf('a')
let returnValue1 = ary.indexOf('a',4)
let returnValue2 = ary.indexOf(NaN) //无法找到 NaN

console.log(returnValue,'&',returnValue1,'&'<
4000
/span>,returnValue2) //3 '&' -1 '&' -1


Array.prototype.lastIndexOf()

- 作用: 查询数组里面的某个元素最后出现的位置

- 参数:
lastIndexOf(value[,index])


- 返回值: 如果查询的元素存在 返回当前元素的索引; 如果查询的元素不存在 返回 -1

- 原有数组是否改变: 否

lastIndexOf(value[,index])
数组里面的某个元素最后出现的位置 从数组的[,index]开始往前查询 默认从最后一个

let ary = ['a','b','c','b']
let returnValue  = ary.lastIndexOf('b')
let returnValue1 = ary.lastIndexOf('b',2) //从索引为2的往前查找 所以最后一个'b' 的索引为 1
let returnValue2 = ary.lastIndexOf('c',-3)

console.log(returnValue,'&',returnValue1,'&',returnValue2) //3 '&' 1 '&' -1


Array.prototype.includes()

- 作用: 判断一个数组是否包含某个元素

- 参数:
includes(value[, start])


- 返回值: 如果包含则返回
true
,否则返回
false


- 原有数组是否改变: 否

indexOf()
相比
includes
可以找到
NaN


let ary = [1,2,3,'a','b','c',NaN]
let returnValue = ary.includes(NaN)
console.log(returnValue) //true


Array.prototype.find()

- 作用: 返回数组中符合函数规则的第一个元素的值

- 参数:
find(callback[,thisArg])


- 返回值: 如果有符合的返回这个元素的值, 没有符合的返回
undefined


- 原有数组是否改变: 否

callback(item, index ,array)
可以传入3个参数(按需传入) item:当前元素 index:当前元素的索引 array:原数组

let ary = [1,3,5,66,8,99]
let returnValue = ary.find(function(item,index){
return item >= 66
})
console.log(returnValue) //66


thisArg
(可选参数) 指定
callback
this
值 注:
callback
不能使用箭头函数 因为箭头函数绑定了
this


let ary = [1,3,5,66,8,99]
ary.find(function(item,index){
console.log(this) //[1,3,5,66,8,99]
},ary)

// 不能使用箭头函数
ary.find((item) => {
console.log(this) //undefined
},ary)


Array.prototype.findIndex()

- 作用: 返回数组中符合函数规则的第一个元素的索引

- 参数:
findIndex(callback[,thisArg])


- 返回值: 如果有符合的返回这个元素的索引, 没有符合的返回
-1


- 原有数组是否改变: 否

callback(item, index ,array)
可以传入3个参数(按需传入) item:当前元素 index:当前元素的索引 array:原数组

let ary = [1,3,5,66,8,99]
let returnValue = ary.findIndex(function(item,index){
return item >= 66
})
console.log(returnValue) //3


thisArg
(可选参数) 指定
callback
this
值 注:
callback
不能使用箭头函数 因为箭头函数绑定了
this


Array.prototype.filter()

- 作用: 过滤数组中符合函数规则的元素

- 参数:
filter(callback[,thisArg])


- 返回值: 把符合规则的元素组成一个新的数组返回,如果没有则返回空数组

- 原有数组是否改变: 否

callback(item, index ,array)
可以传入3个参数(按需传入) item:当前元素 index:当前元素的索引 array:原数组

let ary = [1,3,5,66,8,99]
let returnValue = ary.filter((item) => {
return item > 5
})
console.log(returnValue) //[66, 8, 99]


thisArg
(可选参数) 指定
callback
this
值 注:
callback
不能使用箭头函数 因为箭头函数绑定了
this
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: