10

I am having an issue where .find() is not working on my Chrome browser when I use it in AngularJS. This was my original question: AngularJS: Array.prototype.find() does not work in Chrome

It's failing on this line:

      console.log($scope.products_colors); // prints `[Object]0: Objectlength: 1__proto__: Array[0]concat: function concat() { [native code] }constructor: function Array() { [native code] }every: function every() { [native code] }filter: function filter() { [native code] }forEach: function forEach() { [native code] }indexOf: function indexOf() { [native code] }join: function join() { [native code] }lastIndexOf: function lastIndexOf() { [native code] }length: 0map: function map() { [native code] }pop: function pop() { [native code] }push: function push() { [native code] }reduce: function reduce() { [native code] }reduceRight: function reduceRight() { [native code] }reverse: function reverse() { [native code] }shift: function shift() { [native code] }slice: function slice() { [native code] }some: function some() { [native code] }sort: function sort() { [native code] }splice: function splice() { [native code] }toLocaleString: function toLocaleString() { [native code] }toString: function toString() { [native code] }unshift: function unshift() { [native code] }__proto__: Object mens_detail_controller.js?body=1:24`

      $scope.selected_color = $scope.products_colors.find(function(el) {
        return el.id == 91;
      });

I know it's failing on .find, because the code works when I replace it with something else.

Is there an alternative to looking through an array and grabbing the first element with a certain condition in javascript?

8
  • 10
    xs.filter(f)[0] Commented Sep 22, 2014 at 19:17
  • Why not patch it in? developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Sep 22, 2014 at 19:19
  • Filter seems to work fine. Better than patching it in. Weird since docs say .find is compatible with chrome developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Sep 22, 2014 at 19:20
  • Why is .filter() better than patching it? It's potentially less efficient since it has to search the entire Array, whereas .find() stops as soon as it finds a match. Commented Sep 22, 2014 at 19:24
  • ...the MDN docs are wrong. Chrome may have it under an experimental flag, but they currently don't as a release since ECMAScript 6 isn't finalized yet. Commented Sep 22, 2014 at 19:25

2 Answers 2

18

As pointed out by elclanrs in the comments, xs.filter(f)[0] workes as an alternative to xs.find(f).

Sign up to request clarification or add additional context in comments.

4 Comments

what is xs sd and f ?
xs is the name of the array, f is the name of the function used to compare the elements of that array.
so basically we need to write down own logic to filter.
I know this is old but @Apoorv: You already hat to write down own logic to use "find", since the function requires a function as its parameter. But you can easily replace find with filter, since both functions have the same method signature. Filter returns an array, so simply access the first element. Find automatically returns the first found element.
11

Here is MDN's polyfill:

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;
  };
}

5 Comments

I see that the MDN polyfill is broken. It's supposed to have a i in list condition before it invokes the callback.
...or maybe not. The current draft doesn't have the HasProperty check that the other iterator methods do. That's weird. The current FF implementation does do the check though.
...though the description states "... find calls predicate once for each element present in the array...", so the check is implied. It's just not present in the algorithm.
@cookiemonster I'm not very used to ES6 yet so I'm not sure what you are talking about, but feel free to edit my answer (and MDN) if something is broken.
Your answer is fine since it reflects the current state of MDN, which reflects the current state of the spec (sort of), which is not finalized. It's just odd that they left the HasProperty check out of the algorithm. All the others, like .forEach(), .filter(), etc do the test, but .find() and .findIndex() don't show that step in the algorithm even though it's implied in the description. I'm guessing it'll be corrected eventually, at which point it'll be like: if (i in this && predicate.call(thisArg, value, i, list)) {...

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.