var books = [{title: "showings", Author: "Julian of Norwich", checkouts:45}, {Ti Tle: "The Triads", Author: "Gregory Palamas", checkouts:32}, {title: "The Praktikos", Author: "Evagrius ponticus", checkouts:29}];function dosum (arr) {var total = 0;if (!arr) return Total;for (var i = 0, Ilen = arr.length; i < Ilen; i + +) {var Arri = arr[i];total + = arri.checkouts;} return total;} Ouputs:106console.log (dosum (books)); function Dosum (arr) {return Arr.map (function (item) {return item.checkouts;}). Reduce (function (prev, cur) {return prev + cur;});} var relarray = [["Viola", "Orsino"], ["Orsino", "Olivia"], ["Olivia", "Cesario"]];var Relmap = Relarray.reduce (f Unction (Memo, Curr) {memo[curr[0]] = curr[1]; return memo;},{});//Note there is an initial value here/*outputs: {"Viola": "Orsino", "Orsino": "Olivia", "Olivia": "Cesario"}*/console.log (RELMAP); Array.prototype.reduce = Array.prototype.reduce | | Function (callback, opt_initialvalue) {' Use strict '; if (null = = = This | | ' undefined ' = = = typeof this) {//@ the moment all modern browsers, which support strict mode, has//native IM Plementation of Array.prototype.reduce. For instance, IE8//Does isn't support strict mode, so this check is actually useless. throw new TypeError (' Array.prototype.reduce called on null or undefined '); } if (' function '!== typeof callback) {throw new TypeError (callback + ' is not a function '); } var index, value, length = This.length >>> 0, isvalueset = false; if (1 < arguments.length) {value = Opt_initialvalue; Isvalueset = true; } for (index = 0; length > index; ++index) {if (This.hasownproperty (index)) {if (Isvalueset) { Value = Callback (VAlue, This[index], index, this); } else {value = This[index]; Isvalueset = true; }}} if (!isvalueset) {throw new TypeError (' Reduce of empty array with no initial value '); } return value; };}
Look at the above examples to be clear.
Array Syntax series: Array.prototype.reduce