list - Implement `[1,2].contains(2)` in JavaScript -


this question has answer here:

essentially require:

if( [1,2].contains(2) ) {...} 

i surprised can't find simple solution this.

is possible add contains method javascript's list type?

underscore's similar functions: _.contains vs. _.some , _.map vs _.each offers:

_.contains([1,2], 2); 

however, _ jquery, , still rather clumsy.

the solution of tushar right, recomend way:

object.defineproperty(array.prototype, 'contains', {     writable: true,     configurable: true,     enumerable: false,     value: function(val) {         return this.indexof(val) !== -1;     } }; 

the reason many libraries depend on construction

for (var in array) ... 

if define function without setting enumerable property false, such cycles eterate through function well. enumerable = false prevents that.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -