The find () function of Array does not work in IE11

1

I have the following code and IE11 does not work

var tipo = {datos:[
      {id:'',color:'#F9F9F9',icon:'  ',name:'system'},
      {id:'20',color:'#a7c5e8',icon:'  ',name:'company'},
      {id:'3',color:'#DDDDDD',icon:'<img src="in-mail.png"> ',name:'mail_in'},
      {id:'1',color:'#DDDDDD',icon:'<img src="out-mail.png"> ',name:'mail_out'},
      {id:'4',color:'#DDDDDD',icon:'<img src="in-call.png"> ',name:'call_in'},
      {id:'2',color:'#DDDDDD',icon:'<img src="out-call.png"> ',name:'call_out'}
    ]};

var response = tipo.datos.find(function(o) { return o.id == value.tipo; }).name;

Someone could help me solve this problem or give me an idea of how to solve it. Thank you very much !!!

    
asked by Jorge Bravo 20.12.2018 в 11:10
source

2 answers

2

Since IE does not implement the Array.prototype function. find you could add it when you need something like this:

if (Array.prototype.find == undefined) {
  Array.prototype.find = function (callback,self) {
   let fn=callback.bind(self || this);
   for (let i = 0; i < this.length;i++) {
     if (fn(this[i])) {
       return this[i];
     }
   }
   return undefined;
  };
}

var tipo = {
  datos:[
    {id:'',color:'#F9F9F9',icon:'&nbsp;&nbsp;',name:'system'},
    {id:'20',color:'#a7c5e8',icon:'&nbsp;&nbsp;',name:'company'},
    {id:'3',color:'#DDDDDD',icon:'<img src="in-mail.png"> ',name:'mail_in'},
    {id:'1',color:'#DDDDDD',icon:'<img src="out-mail.png"> ',name:'mail_out'},
    {id:'4',color:'#DDDDDD',icon:'<img src="in-call.png"> ',name:'call_in'},
    {id:'2',color:'#DDDDDD',icon:'<img src="out-call.png"> ',name:'call_out'}
  ]
};

var response = tipo.datos.find(function(o) { return o.id == '4'; }).name;
console.log(response);
    
answered by 20.12.2018 / 11:55
source
2

Indeed, find() is not available in IE11 . On the Mozilla page they propose this polyfill

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;
    },
    configurable: true,
    writable: true
  });
}

var tipo = {datos:[
      {id:'',color:'#F9F9F9',icon:'&nbsp;&nbsp;',name:'system'},
      {id:'20',color:'#a7c5e8',icon:'&nbsp;&nbsp;',name:'company'},
      {id:'3',color:'#DDDDDD',icon:'<img src="in-mail.png"> ',name:'mail_in'},
      {id:'1',color:'#DDDDDD',icon:'<img src="out-mail.png"> ',name:'mail_out'},
      {id:'4',color:'#DDDDDD',icon:'<img src="in-call.png"> ',name:'call_in'},
      {id:'2',color:'#DDDDDD',icon:'<img src="out-call.png"> ',name:'call_out'}
    ]};

var response = tipo.datos.find(function(o) { return o.id == "20"; }).name;

console.log(response);

You can also use, as you were told, filter ()

var tipo = {datos:[
          {id:'',color:'#F9F9F9',icon:'&nbsp;&nbsp;',name:'system'},
          {id:'20',color:'#a7c5e8',icon:'&nbsp;&nbsp;',name:'company'},
          {id:'3',color:'#DDDDDD',icon:'<img src="in-mail.png"> ',name:'mail_in'},
          {id:'1',color:'#DDDDDD',icon:'<img src="out-mail.png"> ',name:'mail_out'},
          {id:'4',color:'#DDDDDD',icon:'<img src="in-call.png"> ',name:'call_in'},
          {id:'2',color:'#DDDDDD',icon:'<img src="out-call.png"> ',name:'call_out'}
        ]};

let response = tipo.datos.filter(function (x) {
         return x.id === "20"
    })[0];
    
console.log(response.name);

Keep in mind that filter returns a array with the found item so you'll have to access it with [0] .

    
answered by 20.12.2018 в 11:45