Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change find method for a string?

Tags:

javascript

How can i replace find method. It is used incorrectly. IE don't see var mail.

var mail = textObj.products.find(function ( itm ) { return itm.name == c }).mail
like image 288
gainsky Avatar asked Nov 25 '25 22:11

gainsky


2 Answers

Array.prototype.find() is not yet supported in IE and Opera (source).

If you want to use it, you should insert the Polyfill function for it:

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

Alternatively, you can use the Array.prototype.filter() function , which is available in IE9 (and up).

like image 133
Jordumus Avatar answered Nov 28 '25 10:11

Jordumus


This is experimental technology and is not supported by every browser. You should use filter instead:

textObj.products.filter(function ( itm ) { return itm.name == c })[0].mail
like image 44
smnbbrv Avatar answered Nov 28 '25 12:11

smnbbrv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!