最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Array.prototype.find() is undefined - Stack Overflow

programmeradmin5浏览0评论

In here

it says that this should work:

function isPrime(element, index, array) {
    var start = 2;
    while (start <= Math.sqrt(element)) {
        if (element % start++ < 1) return false;
    }
    return (element > 1);
}

console.log( [4, 5, 8, 12].find(isPrime) ); // 5

But I end up having an error:

TypeError: undefined is not a function

Why is that?

P.S.

I'm trying not to use underscorejs library since the browsers are supposed to support functions like find() already.

In here

it says that this should work:

function isPrime(element, index, array) {
    var start = 2;
    while (start <= Math.sqrt(element)) {
        if (element % start++ < 1) return false;
    }
    return (element > 1);
}

console.log( [4, 5, 8, 12].find(isPrime) ); // 5

But I end up having an error:

TypeError: undefined is not a function

Why is that?

P.S.

I'm trying not to use underscorejs library since the browsers are supposed to support functions like find() already.

Share Improve this question asked Jun 10, 2014 at 14:28 sesses 13.3k31 gold badges129 silver badges239 bronze badges 2
  • 1 From the first link in your question: This is an experimental technology, part of the Harmony (ECMAScript 6) proposal. Scroll to the bottom of the page, and you will notice only Firefox supports it so far. – Frédéric Hamidi Commented Jun 10, 2014 at 14:30
  • 1 omg. why do they even think about it. ok. unserscorejs then. – ses Commented Jun 10, 2014 at 14:32
Add a ment  | 

3 Answers 3

Reset to default 6

Use the polyfill instead, just copy-paste the following code (from this link) to enable the find method:

if (!Array.prototype.find) {
  Object.defineProperty(Array.prototype, 'find', {
    enumerable: false,
    configurable: true,
    writable: true,
    value: function(predicate) {
      if (this == null) {
        throw new TypeError('Array.prototype.find called on null or undefined');
      }
      if (typeof predicate !== 'function') {
        throw new TypeError('predicate must be a function');
      }
      var list = Object(this);
      var length = list.length >>> 0;
      var thisArg = arguments[1];
      var value;

      for (var i = 0; i < length; i++) {
        if (i in list) {
          value = list[i];
          if (predicate.call(thisArg, value, i, list)) {
            return value;
          }
        }
      }
      return undefined;
    }
  });
}

Instead of .find(...), you also can simply use .filter(...)[0]. (For IE >= 9)

Example:

function isEven(x) {
  return x % 2 == 0;
}
console.log([3, 4, 5].find(isEven)); // 4
console.log([3, 4, 5].filter(isEven)[0]); // 4

Reference: Array.prototype.filter()

As mentioned above, the Array.prototype.find method is an experimental feature proposed for ECMAScript 6. But if you want to use it, and you want a short polyfill that does the job, you can use this (from here):

if (!Array.prototype.find) {
  Array.prototype.find = function (callback, thisArg) {
    "use strict";
    var arr = this,
        arrLen = arr.length,
        i;
    for (i = 0; i < arrLen; i += 1) {
        if (callback.call(thisArg, arr[i], i, arr)) {
            return arr[i];
        }
    }
    return undefined;
  };
}
发布评论

评论列表(0)

  1. 暂无评论