Array Syntax series: Array.prototype.reduce

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.