javascript中Array数组的迭代方法实例分析_javascript技巧

WBOY
Release: 2016-05-16 16:15:49
Original
1248 people have browsed it

本文实例讲述了javascript迭代的方法。分享给大家供大家参考。具体实现方法如下:

复制代码代码如下:
//filter() 利用指定的函数确定是否返回数组中包涵的某一项
var num = [1,2,3,4,5,6,12];

num.filter(function(item, index, array){
return (item > 2); //[3, 4, 5, 6, 12]
});

//map() 返回一个数组,数组中每一项都是在原始数组中的对应项上运行传入参数的结果

var num = [1,2,3,4,5,4,3,2,1];
num.map(function(item, index, array){
return (item * 2); //[2, 4, 6, 8, 10, 8, 6, 4, 2]
});

//every() some() ,查询数组中的某个项是否符合某个条件 every()必须传入的参数每一个都返回true,结果才为true;some()方法
//只要有一个为true,结果就为true

var num = [1,2,3,4,5,4,3,2,1];
num.every(function(item, index, array){
return (item > 2); //false
});

num.some(function(item, index, array){
return (item > 2); //true
})

//forEach() 对数组的每一项传入参数,没有返回值
var num = [1,2,3,4,5,4,3,2,1];
num.forEach(function(item, index, array){
return item;
})

希望本文所述对大家的javascript程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!