Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: ' -> "

...

Code Block
languagejavascript
themeConfluence
f = [7,4,3,5,2,1,6];
f.collect(function(a){(a < 5) => "x" | "y"})
//returns {'x' : [4,3,2,1],
//         'y' : [7,5,6]}
f.collect(function(a){(a % 2) => "odd" | "even"})
//returns {'even' : [4,2,6],
//         'odd' : [7,3,5,1]}

employees = [{'"name'" : '"Ron'", '"dept'": '"marketing'"}, 
             {'"name'" : '"Steve'", '"dept'" : '"executive'"}, 
             {'"name'": '"Mark'", '"dept'": '"engr'"},
             ...
            ];
employees_by_dept = employees.collect(function(a){a{'"dept'"}});

filter()

The filter() operator takes a function as its only argument returns an array. The new array contains any members of the original array for which the function evaluates to true. The function given as the argument must take one argument and return a Boolean value. The length of the new array will be less than or equal to the length of the original array. For example:

...

Code Block
languagejavascript
themeConfluence
j = [{"a" : 4, "b" : 6},
     {"a" : 7, "f" : 8},
     {"a" : 1, "d" : 9}];
j.reduce(function(a,b){a * b{"a"}}, 1) // returns 28
$x[$i] = {
   'val' => 28,
   'type' => 'num'
};

reverse()

...

reverse()

The reverse() operator reverses the order of the array. 

...

Code Block
languagejavascript
themeConfluence
a = ['"corn'",'"tomato'",'"tomato'",'"tomato'",'"sprouts'",'"lettuce'",'"sprouts'"];

c = a.slice(1,4);                   // c = ['"tomato'",'"tomato'",'"tomato'",'"sprouts'"]
d = a.slice(2);                		// d = ['"corn'",'"tomato'",'"tomato'"]
g = a.slice(14)                		// d = undef
h = a.slice(0,0);                	// d = ['"corn'"]

splice()

The splice() operator creates a new array that is the result of deleting, inserting, or replacing elements in the target array. The operators takes the following arguments:
  • the zero-based index of where to start the splice
  • the number of elements to remove at the location given by the first argument
  • an optional value to be spliced in the array at the location given by the first argument 
The following example shows elements being removed from an array:


Code Block
languagejavascript
themeConfluence
a = ['"corn'",'"tomato'",'"tomato'",'"tomato'",'"sprouts'",'"lettuce'",'"sprouts'"];

c = a.splice(1,4); // c = ['"corn'",'"lettuce'",'"sprouts'"]

If the operational third argument is included it will be inserted. If the argument is an array, the elements of the array will all be inserted. 

Code Block
languagejavascript
themeConfluence
a = ['"corn'",'"tomato'",'"sprouts'",'"lettuce'",'"sprouts'"];
b = ['"corn'",'"tomato'"];

c = a.splice(2, 0, b); // c = ['"corn'",'"tomato'",'"corn'",'"tomato'",'"sprouts'",'"lettuce'",'"sprouts'"]

If the third argument is not an array, its value be inserted. 

Code Block
languagejavascript
themeConfluence
a = ['"corn'",'"tomato'",'"sprouts'",'"lettuce'",'"sprouts'"];

c = a.splice(2, 0, '"liver'"); // c = ['"corn'",'"tomato'",'"liver'",'"sprouts'",'"lettuce'",'"sprouts'"]

In the preceding examples, we've been removing zero elements (i.e. simply inserting). If the second argument is non-zero, then that many elements will be removed before the elements of the third argument are inserted at the location where the elements were removed:

Code Block
languagejavascript
themeConfluence
a = ['"corn'",'"tomato'",'"sprouts'",'"lettuce'",'"sprouts'"];

c = a.splice(2, 2, '"liver'"); // c = ['"corn'",'"tomato'",'"liver'",'"sprouts'"]

If the second argument is larger than the remaining elements in the array, the array will be truncated at the location given by the first parameter:

Code Block
languagejavascript
themeConfluence
a = ['"corn'",'"tomato'",'"tomato'",'"tomato'",'"sprouts'",'"lettuce'",'"sprouts'"];

c = a.splice(1,10); // c = ['"corn'"]


sort()

The sort() operator takes an optional argument and returns an array that is the original array sorted according to the following criteria:

...