2016-06-13 26 views
17
<!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> 

Il codice pubblicato sopra funziona correttamente su Firefox e Chrome ma ottengo un errore in Internet Explorer. Messaggio di errore:"Oggetto non supporta proprietà o metodo 'trova'" in IE

Object doesn't support property or method 'find'

+0

Quale versione (s) di IE stai testando con? Inoltre, è in modalità standard, modalità di compatibilità o modalità stranezze? – Simba

+0

hi @Simba sto usando IE versione 11.0.9660.18321 –

+0

per verificare quali browser supportano le funzionalità, http://www.caniuse.com è molto utile, solo per riferimento futuro. –

risposta

17

Si utilizza il metodo JavaScript array.find(). Si noti che questo è JS standard e non ha nulla a che fare con jQuery. In realtà, l'intero codice nella domanda non fa assolutamente uso di jQuery.

È possibile trovare la documentazione per array.find() qui: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find

Se si scorre alla fine di questa pagina, si nota che essa ha del browser informazioni di supporto, e vedrete che si afferma che IE non supporta questo metodo.

Ironia della sorte, il modo migliore per aggirare questo sarebbe utilizzare jQuery, che ha funzionalità simili supportate da tutti i browser.

-4

Per trovare supporto metodo del browser inizia da IE 12.

Si prega di verificare sul link qui sotto.

Click Here

+8

IE12 non esiste. Il browser che stai guardando con il blu 'e "Il logo ora si chiama Edge ed è significativamente diverso dal suo predecessore IE. – Simba

5

Come accennato array.find() non è supportato in IE.

Tuttavia si può leggere su un Polyfill qui:

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

Questo metodo è stato aggiunto alle specifiche ECMAScript 2015 e potrebbe non essere ancora disponibile in tutte le implementazioni JavaScript. Tuttavia, è possibile Polyfill Array.prototype.find con il seguente frammento:

Codice:

// 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; 
    } 
    }); 
} 
Problemi correlati