数组封装新方法
//查找数组中符合条件的元素 Array.prototype.where = function (condition) { var ret = []; for (var i = 0; i < this.length; i++) { if (condition(this[i])) { ret.push(this[i]); } } return ret; };
//查找数组中第一个符合条件的元素 Array.prototype.first = function (condition) { for (var i = 0; i < this.length; i++) { if (condition(this[i])) { return this[i]; } } return undefined; }
//从数组中选择前n个元素 Array.prototype.top = function (count) { var ret = []; count = count < this.length ? count : this.length; for (var i = 0; i < count; i++) { ret.push(this[i]); } return ret; }
本文来自博客园,作者:喆星高照,转载请注明原文链接:https://www.cnblogs.com/houxianzhou/p/17095294.html

浙公网安备 33010602011771号