Hızlı yanıt: kod örneği
stackoverflow.com how-do-i-check-if-an-array-includes-a-value-in-javascript
Modern browsers have , which does exactly that and is widely supported by everyone except IE:You can also use , which is less direct, but doesn't require polyfills for outdated browsers.Many frameworks also offer similar methods:
Array#includes
console.log(['joe', 'jane', 'mary'].includes('jane')); // true
Array#indexOf
console.log(['joe', 'jane', 'mary'].indexOf('jane') >= 0); // true
- jQuery:
$.inArray(value, array, [fromIndex])
- Underscore.js: (also aliased as
_.contains(array, value)
and_.include
)_.includes
- Dojo Toolkit:
dojo.indexOf(array, value, [fromIndex, findLast])
- Prototype:
array.indexOf(value)
- MooTools:
array.indexOf(value)
- MochiKit:
findValue(array, value)
- MS Ajax:
array.indexOf(value)
- Ext:
Ext.Array.contains(array, value)
- Lodash: (is
_.includes(array, value, [from])
prior 4.0.0)_.contains
- Ramda:
R.includes(value, array)