2

I'm looking for an alternative for the find() method of an array. I'm using it on Android Browser and Array.prototype.find() doesn't work there. Definition Array Support

var test= this.arrayOfValues.find(function (value) {
            return (value.name === device.value)
});
2

1 Answer 1

2

If you do not care so much about programming your own, and if indexOf is somehow not usable, have a look at Underscore.js#find. As an alternative, as @NinaScholz recommended in the comments, use the Polyfill from mozilla.org:

if (!Array.prototype.find) {
  Array.prototype.find = 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++) {
      value = list[i];
      if (predicate.call(thisArg, value, i, list)) {
        return value;
      }
    }
    return undefined;
  };
}
Sign up to request clarification or add additional context in comments.

1 Comment

Using Polyfill from mozilla work's very well. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.