Home > Web Front-end > JS Tutorial > body text

Example analysis of iteration method of Array array in javascript_javascript skills

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

The example in this article describes the method of javascript iteration. Share it with everyone for your reference. The specific implementation method is as follows:

Copy code The code is as follows:
//filter() uses the specified function to determine whether to return a certain value contained in the array Item
var num = [1,2,3,4,5,6,12];

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

//map() returns an array, each item in the array is the result of running the passed parameters on the corresponding item in the original array

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(), query whether an item in the array meets a certain condition. Every() must pass in every parameter to return true, and the result will be true; some() method
//As long as one of them is true, the result is 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() passes parameters to each item in the array and has no return value
var num = [1,2,3,4,5,4,3,2,1];
num.forEach(function(item, index, array){
return item;
})

I hope this article will be helpful to everyone’s JavaScript programming design.

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template