find.js 478 B

1234567891011121314151617181920212223
  1. 'use strict';
  2. function find(array, predicate, context) {
  3. if (typeof Array.prototype.find === 'function') {
  4. return array.find(predicate, context);
  5. }
  6. context = context || this;
  7. var length = array.length;
  8. var i;
  9. if (typeof predicate !== 'function') {
  10. throw new TypeError(predicate + ' is not a function');
  11. }
  12. for (i = 0; i < length; i++) {
  13. if (predicate.call(context, array[i], i, array)) {
  14. return array[i];
  15. }
  16. }
  17. }
  18. module.exports = find;