数组常用方法(3)ES5.html

<script type="text/javascript">
var arr=['hello','world','Jack','Rose','world']

//1.indexOf() 查看数组中是否存在该内容
//第一种使用方式
//语法:数组.indexOf(需要查找的内容)
//不改变原始数组
//返回值:
// 如果找到,返回的是这个数据的索引位置(只能找到第一个)
// 如果没有找到,返回值是-1
var res=arr.indexOf('Jack')
console.log(arr) //(5) ["hello", "world", "Jack", "Rose", "world"]
console.log(res) //2
//document.writeln(res)
//document.writeln(arr)

//第二种使用方式
//语法:数组.indexOf(需要查找的内容,从哪一个索引开始查找)
//不改变原始数组
//返回值:
// 如果找到,返回的是这个数据的索引位置(只能找到第一个)
// 如果没有找到,返回值是-1
var res=arr.indexOf('world',0)
console.log(arr) //(5) ["hello", "world", "Jack", "Rose", "world"]
console.log(res) //1


//2.forEach() 用来遍历数组的,ES5对for循环的扩展
//语法:数组.forEach(传递一个函数)
//数组的多少项,里面这个函数就执行多少回
//返回值:forEach()方法没有返回值 (undefined)
var res= arr.forEach(function (item,index,arr){
//arr有多少项,函数就执行多少回
// console.log('我执行了')

//在这个函数的内部,有三个形参可接收,会在函数遍历过程中自动传递
//会自动赋上实参,我们 只管形参

//itme 项,表示数组中的每一个成员
console.log(item)/*
* hello
* world
* Jack
* Rose
* world
*/
//index 索引,表示循环过程中的索引
console.log(index)/*
* 0
* 1
* 2
* 3
* 4
*/
//arr 原始数组,表示原始数组
console.log(arr)/*
* (5) ["hello", "world", "Jack", "Rose", "world"]
* (5) ["hello", "world", "Jack", "Rose", "world"]
* (5) ["hello", "world", "Jack", "Rose", "world"]
* (5) ["hello", "world", "Jack", "Rose", "world"]
* (5) ["hello", "world", "Jack", "Rose", "world"]
*/

})
console.log(res) //undefined


//3.map() 用来映射数组(地图),对原始数组中的每一项进行操作,操作完成后返回一个新数组
//语法:数组.map(传递一个函数)
//返回值:是一个新数组
//不改变原始数组
var res=arr.map(function (item,index,arr){
//在这个函数的内部,有三个形参可接收,会在函数遍历过程中自动传递
//会自动赋上实参,我们 只管形参
console.log(item)
console.log(index)
console.log(arr)
//item,index,arr 与上边forEach() 一样

//这个地方是一个函数、随着循环这个函数不停的执行
//得到 nudefined 是因为这个函数没有返回值
//return 1 那他就得到 5个1
return item+'A'
})
console.log(arr) //(5) ["hello", "world", "Jack", "Rose", "world"]
console.log(res) //这个是没有return tem+'A'之前: (5) [undefined, undefined, undefined, undefined, undefined]
console.log(res) //(5) ["helloA", "worldA", "JackA", "RoseA", "worldA"]



//4.filter() 对原始数组进行筛选,把满足条件的筛选出来 ,放在一个新的数组里面返回
//语法:数组.filter(传递一个函数)
//不改变原始数组
//返回值:返回满足条件的新数组,如果都不满足、则返回空数组
var arr2=[1,2,3,4,5,6]
var res= arr2.filter(function(itme,indrx,arr){
//函数会随着循环不停的执行
//接收 三个参数

//使用return 方式来书写条件
//如果后面的条件满足,那么就是return true
//如果后面有条件满足,那么就是return false
//如果条件是true,那么这个item就是被放入新的数组
//如果条件是false,那么这个item不会被放入新的数组

return itme>2

})
console.log(arr2) //(6) [1, 2, 3, 4, 5, 6]
console.log(res) //(4) [3, 4, 5, 6]


//5.some() <某一个>对原始数组进行遍历,只要有一项满足条件、就返回true,如果每一项都不满足条件、则返回false
//语法;数组.some(传递一个函数)
//不改变原始数组
//返回值:返回布尔
var res=arr2.some(function(item,index,arr){
//函数会随着循环不停的执行
//接收 三个参数

//条件以 return 形式返回 、随着循环,这个条件会被返回length次

return item >10
})
console.log(arr2) //(6) [1, 2, 3, 4, 5, 6]
console.log(res) //false


//6.every() <每一个>对原始数组进行遍历,需要每一项都满足条件、才返回true,只要有一项不满足条件,则返回false
//语法:数组.every(传递一个函数)
//不改变原始数组
//返回值:返回布尔
var res=arr2.every(function(item,index,arr){
//函数会随着循环不停的执行
//接收 三个参数

//条件以 return 形式返回 、随着循环,这个条件会被返回length次

return item >=1
})
console.log(arr2) //(6) [1, 2, 3, 4, 5, 6]
console.log(res) //true
</script>

posted @ 2020-04-16 08:09  阿向向  阅读(153)  评论(0编辑  收藏  举报