ECMAScript5数组新增方法
来源:互联网 发布:商品与条形码数据库 编辑:程序博客网 时间:2024/04/29 17:44
forEach(function(element,index,arr))
改变原数组,遍历数组
var a = ["a","b",1,2,3];a.forEach(function(element,index,arr){console.log(index+":"+element);}); /*0:a 1:b 2:1 3:2 4:3 */
map: map(function(element));
映射数组,返回新的数组,新数组索引结构和原数组一致,原数组不变
var a = ["a","b",1,2,3];var b = a.map(function(element){return ""+element+1;}); console.log(b); //["a1", "b1", "11", "21", "31"]
filter(function(element))
过滤数组返回新数组
var a = ["a","b",1,2,3];var b = a.filter(function(element,index,arr){return (typeof element) == "string";});console.log(b); //["a", "b"]
every(function(element,index,arr)): &&与运算
some(function(element,index,arr)):||运算
every 查看数组的元素是否全部符合某个条件,如果全部符合返回true,否则返回falsesome 查看数组的元素是否符合某个条件,如果有至少一个符合,则返回true,否则返回false
var a = ["a","b",1,2,3];var b = a.filter(function(element,index,arr){return (typeof element) == "string";});console.log(b); //["a", "b"] var a = [1,2,3];var b = a.filter(function(element,index,arr){return (typeof element) == "number";});console.log(b); //["a", "b"]indexOf/lastIndexOf
这两个方法用于查找数组内指定元素位置,查找到第一个后返回其索引,没有查找到返回-1,indexOf从头至尾搜索,lastIndexOf反向搜索。
Array.isArray()
Array对象的一个静态函数,用来判断一个对象是不是数组
reduce(function(v1,v2),value)/.reduceRight(function(v1,v2),value)
遍历数组,调用回调函数,将数组元素组合成一个值,reduce从索引最小值开始,reduceRight反向,方法有两个参数 1)回调函数:把两个值合为一个,返回结果; 2)value,一个初始值,可选。
var a=[1,2,3,4,5]; var b = a.reduceRight(function(x,y){return x*y;});console.log(b); //120var a=[1,2,3,4,5]; var b = a.reduce(function(x,y){return x*y;});console.log(b); //120var a=[1,2,3,4,5]; var b = a.reduceRight(function(x,y){return x*y;},3);console.log(b); //360var a=[1,2,3,4,5]; var b = a.reduce(function(x,y){return x*y;},3);console.log(b); //360
- ECMAScript5数组新增方法
- ECMAScript5新增array方法
- ECMAScript5中新增数组操作方法
- ECMAscript5中的数组方法
- ECMAScript5新增数组API的一些细节
- ECMAScript5新增Array方法forEach的实现
- ECMAScript5 Object新增的属性和方法
- 【JavaScript】ECMAScript5中新增的数组操作函数
- 你不知道的EcmaScript5数组方法
- 【js学习笔记-043】-- ECMAScript5数组方法
- ECMAScript5中数组迭代方法
- ECMAScript5 中的数组方法 二十八
- ECMAScript5中数组方法的性能问题
- ECMAScript5(ES5)标准中扩展的数组Array方法
- es5新增数组方法
- JavaScript权威指南_57_第7章_数组_7.9-ECMAScript5中的数组方法
- ECMAScript5之前的数组操作
- ES6---数组array新增方法
- 解决EXC_BAD_ACCESS错误的一种方法--NSZombieEnabled
- jquery垂直公告滚动实现代码
- python unit test to assert a method calls sys.exit()
- 产生随机数
- cocos2d-x触摸分发器原理
- ECMAScript5数组新增方法
- STM32芯片命名
- warning: function returns address of local variable
- C指针原理(2)
- java 和javaw 的区别
- 让世界听到你的声音---------如何快速建造一个好看的个人博客
- 各种分布式文件系统简介
- LLDB vs GDB
- 【九度】题目1349:数字在排序数组中出现的次数