Some Functional JQuery bits
less than 1 minute read
Return a processed array, leaving underlying array unaltered
var arr = Array.apply(null, Array(5)).map(function (_, i) {return i;});
var processedArr = $.map(arr, function(el) { return el * 2; } );
// arr = [0, 1, 2, 3, 4]
// processedArr = [0, 2, 4, 6, 8]
Summarize values in an array
var keys = $.unique($.map(objects, function(e) { return e[keyfield] }))
$.map(keys,function(e){
var ret = {};
ret[e]= $.grep(objects, function(gr) {
return gr[keyfield] == e;
}).length;
return ret;
} );
JSFiddle
Modify every row in an array
var arr = Array.apply(null, Array(5)).map(function (_, i) {return i;});
// note that no records change with scalar elements b/c items passed by value
$.each(arr, function(idx, el) { el *= 2; });
// [ 0, 1, 2, 3, 4 ]
var arr1 = [ ['a',1],['b',2],['c',3] ];
$.each(arr, function(idx, el) { el *= 2; });
// [["a", 2], ["b", 4], ["c", 6]]
var arr2 = [ {a: 1}, {a: 2}, {a: 3} ];
// records change with an object
$.each(arr2, function(idx, el) { el.a *= 2; });
// [ {a: 2}, {a: 4}, {a: 6} ];