Array.Prototype.Includesとは
Ecmaスクリプトを読み直してたらArray.prototype.includesというのが実装されていたのを知りました。
2016 2016年6月 冪乗演算子、Array.prototype.includes
というかべき乗演算子もわりと最近だったんですね・・・
配列の中にその値があるかを判定します。
coffee> arr = [1,3,5,7,9]
[ 1, 3, 5, 7, 9 ]
coffee> arr.includes 1
true
coffee> arr.includes 2
false
indexOf でも同じことができますが、こちらはboolが返ってきます。
coffee> arr = [1,3,5,7,9]
[ 1, 3, 5, 7, 9 ]
coffee> arr.indexOf 1
0
coffee> arr.indexOf 2
-1
今まで
!= -1 とかで判定していたかと思います。
IE11はまだ未対応らしい
ecma2016系統が対応されてないということですね。試してみます。
javascript:alert([1,2].includes(1););

何も返ってきませんでした。

Edgeはサポートされてました。
ポリフィル
書きました。
if !Array.prototype.includes
Object.defineProperty Array.prototype, 'includes',
value: (s, index)->
throw new TypeError '"this" is null or not defined' if this == null
o = Object this
len = o.length >>> 0
return false if (len == 0)
n = index | 0
k = Math.max( (if n >= 0 then n else len - Math.abs(n)) , 0)
while (k < len)
return true if o[k] == s
k++
false
includesが使えないときにご利用ください。
鈴木
和服とvapeとСистемаと醗酵とたまごふわふわとカッティングシェイプスとジャージークラブとjuke/fwkに傾倒する人です