IE alternative to Array.prototype.find ()

Earlier I wrote code that maps uploaded files to their respective client IDs and displays them in a table to show which files are assigned to those clients. The problem is I tested this on Chrome and Safari as per the job spec and it works great.

The problem is it doesn't work on IE due to the fact that it doesn't support Array.prototype.find()

and now they have asked for IE compatibility.

I looked at other questions, but the answers were specific to their situation, often giving examples of other ways to do what they were looking for.

What would be the best way to achieve what I am trying to do?

var item = clientList.find(function(item) {

    return item.UniqueID == ClientID;

});

      

+6


source to share


1 answer


You can create your own search function, an important part of which is to break the loop when matching an item.



var data = [{id: 1, name: 'a'}, {id: 2, name: 'b'}];

function altFind(arr, callback) {
  for (var i = 0; i < arr.length; i++) {
    var match = callback(arr[i]);
    if (match) {
      return arr[i];
    }
  }
}

var result = altFind(data, function(e) {
  return e.id == 2;
})

console.log(result)
      

Run codeHide result


+6


source







All Articles