LoginSignup
2
2

More than 5 years have passed since last update.

IE11でArray.findIndexの代替はLodashで回避した

Last updated at Posted at 2018-09-06

ハッシュ配列から要素を引いてくるときに、Array.prototype.findIndex つかってたのですが、IE11で実装されていませんでした... :weary:

favs
favs = [
  {
    "code": "c1",
    "name": "foo"
  },
  {
    "code": "c2",
    "name": "bar"
  }
];
Before
ByCode = function (item) {
  return item.code == this;
}
var code = "c2";
found = favs.findIndex(ByCode, code);
// found == 1

Lodashなら雰囲気を維持したままIE11対応できるのが :ok_hand:

<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
After
var code = "c2";
found = _.findIndex(favs, function(item){
  return item.code == code;
});
// found == 1
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2