Javascript Data Structure Tutorial - Javascript Array Iterator








Array Iterator functions can apply a function to each element of an array.

And it can return one value, or a set of values, or a new array after applying the function to each element of an array.

forEach()

forEach() function takes a function as an argument and applies the called function to each element of an array.

To process each element in an array we can use the forEach() method.

Here is an example of how it works:


function square(num) { 
    console.log(num, num * num); 
} 
var nums = [1,2,3,4,5,6,7,8,9,10]; 
nums.forEach(square); 

The code above generates the following result.





every()

every() applies a Boolean function to an array and returns trueif the function can return true for every element in the array.

We can use the every() method to check if every element in the array is an even number.

Here is an example:


function isEven(num) { /*from   ww  w .j  ava  2s .  c om*/
    return num % 2 == 0; 
} 
var nums = [2,4,6,8,10]; 
var even = nums.every(isEven); 
if (even) { 
    console.log("all numbers are even"); 
} else { 
    console.log("not all numbers are even"); 
} 

The code above generates the following result.





some()

The some() function takes a Boolean function and returns true if at least one of the elements meets the criterion of the Boolean function.

We can use the some() method to check if one of the element in the array is an even number.

For example:


function isEven(num) { // ww  w . ja v  a2  s  . c  o m
    return num % 2 == 0; 
} 
var nums = [1,2,3,4,5,6,7,8,9,10]; 
var someEven = nums.some(isEven); 
if (someEven) { 
    console.log("some numbers are even"); 
} 
else { 
    console.log("no numbers are even"); 
} 
nums = [1,3,5,7,9]; 
someEven = nums.some(isEven); 
if (someEven) { 
    console.log("some numbers are even"); 
} 
else { 
    console.log("no numbers are even"); 
} 

The code above generates the following result.

reduce()

The reduce() function applies a function to an accumulator and the successive elements of an array until the end of the array is reached. The reduce() method returns a single value.

Here is an example of using reduce() to compute the sum of the elements of an array:


function add(runningTotal, currentValue) { 
    return runningTotal + currentValue; 
} 
var nums = [1,2,3,4,5,6,7,8,9,10]; 
var sum = nums.reduce(add); 
console.log(sum);

The code above generates the following result.

We can use reduce() with strings to perform concatenation:


function concat(accumulatedString, item) { 
    return accumulatedString + item; 
}

var words = ["the ", "quick ","brown ", "fox "]; 
var sentence = words.reduce(concat); 
console.log(sentence);

The code above generates the following result.

reduceRight()

JavaScript reduceRight() function works from the right hand side of the array to the left.

reduce() is working from left to right.

The following program uses reduceRight() to reverse the elements of an array:


function concat(accumulatedString, item) { 
    return accumulatedString + item; 
} 
var words = ["the ", "quick ","brown ", "fox "]; 
var sentence = words.reduceRight(concat); 
console.log(sentence);

The code above generates the following result.

map()

The map() function applies a function to each element of an array.

map() returns a new array with the results of the function application.

Here is an example:


function curve(grade) { 
    return grade += 5; 
} 
var grades = [77, 65, 81, 92, 83]; 
var newgrades = grades.map(curve); 
console.log(newgrades);

The code above generates the following result.

Here is an example using strings:


function first(word) { 
    return word[0]; 
} 
var words = ["For","Your","Information"]; 
var acronym = words.map(first); 
console.log(acronym.join(""));

The code above generates the following result.

filter()

The filter() function returns a new array consisting of those elements that satisfy the Boolean function.

Here is an example:


function isEven(num) { /*from  w  w  w .j  av a2  s  .c om*/
    return num % 2 == 0; 
} 
function isOdd(num) { 
    return num % 2 != 0; 
} 


var nums = []; 
for (var i = 0; i < 20; ++i) { 
    nums[i] = i+1; 
} 
var evens = nums.filter(isEven); 
console.log("Even numbers: "); 
console.log(evens); 
var odds = nums.filter(isOdd); 
console.log("Odd numbers: "); 
console.log(odds); 

The code above generates the following result.