数组的查询
2018-04-02 16:44
120 查看
数组常用的查询方法
- 参数:
- 返回值: 把找到的内容当做一个新的数组返回
- 原有数组是否改变: 否
- 参数:
- 返回值: 如果查询的元素存在 返回当前元素的索引; 如果查询的元素不存在 返回 -1
- 原有数组是否改变: 否
- 参数:
- 返回值: 如果查询的元素存在 返回当前元素的索引; 如果查询的元素不存在 返回 -1
- 原有数组是否改变: 否
- 参数:
- 返回值: 如果包含则返回
- 原有数组是否改变: 否
同
- 参数:
- 返回值: 如果有符合的返回这个元素的值, 没有符合的返回
- 原有数组是否改变: 否
- 参数:
- 返回值: 如果有符合的返回这个元素的索引, 没有符合的返回
- 原有数组是否改变: 否
- 参数:
- 返回值: 把符合规则的元素组成一个新的数组返回,如果没有则返回空数组
- 原有数组是否改变: 否
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
相关文章推荐
- 树状数组(区间更新区间查询)
- hdu 1556 Color the ball (树状数组解决区间覆盖+单点查询)
- 二维树状数组(区间修改,单点查询)
- hdu2642-二维树状数组 单点更新 区间查询
- C# mongodb中内嵌文档数组条件查询
- 查询整型数组中第二大的数
- in_array()大数组查询性能问题
- perl获取数据库查询返回数组大小
- 树状数组模板(区间更新单点查询)
- hibernate查询结果转为实体类 数组越界报错
- POJ2155【二维树状数组,区间修改,点查询?】【又被输入输出坑】
- javascript查询(简单的)对象在数组中位置
- php 数组操作(增加,删除,查询,排序)等函数说明第1/2页
- php 数组操作(增加,删除,查询,排序)等函数说明第1/2页
- linq 查询 过滤数据中某个值是否在数组中
- 关于树状数组区间修改区间查询
- 数组为什么可以使用linq查询
- thinkphp数据查询和遍历数组实例
- php+mysql非缓冲查询(如何循环大数组)
- CodeChef CDMU02(数组辅助查询)