Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Alternative to Array.prototype.find()?

Tags:

javascript

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?

like image 905
bigpotato Avatar asked Sep 22 '14 19:09

bigpotato


People also ask

What can I use instead of find in JavaScript?

If you need a list of all matches, then you should use . filter() instead of . find() .

Can I use array prototype at ()?

Array.prototype.at() The at() method takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.

How do you search an array in JavaScript?

Use filter if you want to find all items in an array that meet a specific condition. Use find if you want to check if that at least one item meets a specific condition. Use includes if you want to check if an array contains a particular value. Use indexOf if you want to find the index of a particular item in an array.

Does JavaScript find return array?

find() method in JavaScript returns the value of the first element in the array that satisfies the provided testing method.


2 Answers

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

like image 132
3 revs, 2 users 75% Avatar answered Sep 17 '22 13:09

3 revs, 2 users 75%


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;
  };
}
like image 45
Oriol Avatar answered Sep 17 '22 13:09

Oriol