ES5数组扩展
2024-10-07 19:59:52
ES5给数组对象添加了一些方法, 常用的5个:
1. Array.prototype.indexOf(value) : 得到值在数组中的第一个下标
2. Array.prototype.lastIndexOf(value) : 得到值在数组中的最后一个下标
3. Array.prototype.forEach(function(item, index){}) : 遍历数组
4. Array.prototype.map(function(item, index){ }) : 遍历数组返回一个新的数组
5. Array.prototype.filter(function(item, index){ }) : 遍历过滤出一个新的子数组
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>04_Array扩展</title>
</head>
<body>
<!--
. Array.prototype.indexOf(value) : 得到值在数组中的第一个下标
. Array.prototype.lastIndexOf(value) : 得到值在数组中的最后一个下标
. Array.prototype.forEach(function(item, index){}) : 遍历数组
. Array.prototype.map(function(item, index){}) : 遍历数组返回一个新的数组,返回加工之后的值
. Array.prototype.filter(function(item, index){}) : 遍历过滤出一个新的子数组, 返回条件为true的值
-->
<script type="text/javascript">
/*
需求:
1. 输出第一个6的下标
2. 输出最后一个6的下标
3. 输出所有元素的值和下标
4. 根据arr产生一个新数组,要求每个元素都比原来大10
5. 根据arr产生一个新数组, 返回的每个元素要大于4
*/ var arr = [, , , , , ];
console.log(arr.indexOf());//2
//Array.prototype.lastIndexOf(value) : 得到值在数组中的最后一个下标
console.log(arr.lastIndexOf());//5 //Array.prototype.forEach(function(item, index){}) : 遍历数组
arr.forEach(function (item, index) {
console.log(item, index);
}); //Array.prototype.map(function(item, index){}) : 遍历数组返回一个新的数组,返回加工之后的值
var arr1 = arr.map(function (item, index) {
return item +
});
console.log(arr, arr1); //Array.prototype.filter(function(item, index){}) : 遍历过滤出一个新的子数组, 返回条件为true的值
var arr2 = arr.filter(function (item, index) {
return item >
});
console.log(arr, arr2); </script>
</body>
</html>
最新文章
- 部署React+webpack工程的步骤
- 简单阐述下OC中UIImage三种创建方式~~~
- shell 脚本技巧
- eclispe中在线安装maven插件
- NLTK中文语料库sinica_treebank
- WCF-复合类型使用;传输图片
- IOS--手势控制的使用
- 水果姐逛水果街Ⅰ(codevs 3304)
- 从文章";避免复制与粘贴";到文章";Extract Method";的反思(3)
- 学习下关于ViewStub实例的用法及带Drawable的TextView的妙用
- [C++程序设计]函数的递归调用
- python-----运算符及while循环
- C#后台调用浏览器打开下载连接地址的三种方法
- R语言命令行参数
- Linux System Programming --Chapter Six
- IaaS,PaaS,SaaS 的区别
- Linux 典型应用之远程连接SSH
- 谈谈 在 .Net 生态里为什么没有 Hadoop 系列 ?
- ucos串口通讯模块设计
- SQL Server上DBLINK的创建,其实很简单!(上)