Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Object doesn't support property or method 'find'" in IE

<!DOCTYPE html>  <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">     <title></title>          <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>      <script>          $(document).ready(function () {               var data = [{                 "Id": "SWE",                 "Country": "Sweden",                 "Population": 9592552             }, {                 "Id": "NOR",                 "Country": "Norway",                 "Population": 5084190             }];               function display(e) {                 alert("E" + e);                 var countryData = data.find(function (element, index, array) {                     return element.Id === e;                 });                 alert(countryData.Population);             }             display('SWE');           });       </script> </head> </html> 

The code posted above is working properly on Firefox and Chrome but I get an error in Internet Explorer. Error message:

Object doesn't support property or method 'find'

like image 436
GOPAL YADAV Avatar asked Jun 13 '16 11:06

GOPAL YADAV


2 Answers

Here is a work around. You can use filter instead of find; but filter returns an array of matching objects. find only returns the first match inside an array. So, why not use filter as following;

data.filter(function (x) {          return x.Id === e     })[0]; 
like image 200
Mahib Avatar answered Sep 20 '22 00:09

Mahib


As mentioned array.find() is not supported in IE.

However you can read about a Polyfill here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find#Polyfill

This method has been added to the ECMAScript 2015 specification and may not be available in all JavaScript implementations yet. However, you can polyfill Array.prototype.find with the following snippet:

Code:

// https://tc39.github.io/ecma262/#sec-array.prototype.find if (!Array.prototype.find) {   Object.defineProperty(Array.prototype, 'find', {     value: function(predicate) {      // 1. Let O be ? ToObject(this value).       if (this == null) {         throw new TypeError('"this" is null or not defined');       }        var o = Object(this);        // 2. Let len be ? ToLength(? Get(O, "length")).       var len = o.length >>> 0;        // 3. If IsCallable(predicate) is false, throw a TypeError exception.       if (typeof predicate !== 'function') {         throw new TypeError('predicate must be a function');       }        // 4. If thisArg was supplied, let T be thisArg; else let T be undefined.       var thisArg = arguments[1];        // 5. Let k be 0.       var k = 0;        // 6. Repeat, while k < len       while (k < len) {         // a. Let Pk be ! ToString(k).         // b. Let kValue be ? Get(O, Pk).         // c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).         // d. If testResult is true, return kValue.         var kValue = o[k];         if (predicate.call(thisArg, kValue, k, o)) {           return kValue;         }         // e. Increase k by 1.         k++;       }        // 7. Return undefined.       return undefined;     }   }); } 
like image 39
Ogglas Avatar answered Sep 19 '22 00:09

Ogglas