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
Post a Comment