Java Reference
In-Depth Information
var names = ["Fu", "Li", "Su"];
var namesList = names.reduce(concatReducer);
var namesListRight = names.reduceRight(concatReducer);
print("Names: " + names);
print("Names Reduced List: " + namesList);
print("Names Reduced Right List: " + namesListRight);
Numbers :1,2,3,4,5
Sum: 15
Names: Fu,Li,Su
Names Reduced List: Fu-Li-Su
Names Reduced Right List: Su-Li-Fu
You can also chain these methods to perform complex processing on arrays. The
following code shows how to compute the sum of squares of all positive odd numbers in
an array in one statement:
var nums = [-2, 1, 2, 3, 4, 5, -11];
// Compute the sum of squares of all positive odd numbers
var sum = nums.filter(function (value, index, data) value > 0 && value % 2
!== 0)
.map(function (value, index, data) value * value)
.reduce(function (prev, curr, index, data) prev + curr, 0);
print("Numbers: " + nums);
print("Sum of squares of positive odd elements: " + sum);
Numbers: -2,1,2,3,4,5,-11
Sum of squares of positive odd elements: 35
Array-Like Objects
Arrays have two features that make them distinct from regular objects:
It has a
length property
Its elements have special property names that are integers
You can define any object with these two characteristics and call them array-like
objects. The following code defines an object named list that is an array-like object:
// Creates an array-like object
var list = {"0":"Fu", "1":"Su", "2":"Li", length:3};
 
Search WWH ::




Custom Search