2

Assuming we don't do what I want to do which is to implement Array.prototype.has(), what is conventions for the following...

if (['contacts','calendar','tasks','inbox'].indexOf(kind)!==-1) {

What I don't like is the !==-1, but I also don't like the array.

I used to use a String.prototype.inList(list,<delimiter>) which would actually compute the delimiter if not provided.

Forgive me for bemoaning the collision of zero-based arrays and truthiness. Is there some sneaky way of doing this without indexOf and !==-1, I suppose I could say indexOf(kind)+1 to get the truthiness. What else?

EDIT: To refine what I am looking for -- is a way to handle "is string in list", but in the literal way (quick, dirty, shorthand) and not so much in the way where list is an array, or in a variable like find.inList(list). I am open to prototype solutions, but am more curious about creative hacks like the ~'a b c'.split(' ').indexOf(abc). So maybe String.prototype.hasItem(find,<delimiter>) so that 'a b c'.hasItem(abc)

9
  • 3
    if (~[a,b,c].indexOf(d)), but how about if ([a,b,c].some(is(d)), where is=x=>y=>x===y Commented May 10, 2015 at 1:16
  • 1
    In ES7 Array.prototype.includes exist which will return a boolean. So you don't have to use `.indexOf() !== -``. Polyfill: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented May 10, 2015 at 1:20
  • This is also the behavior of the indexof type operator in.a number of other languages. its a well established convention Commented May 10, 2015 at 1:24
  • @Edwin includes ?? they must be giving getElementsByTagName a run for their money. I am surprised they didn't name it isContainingMemberLike Commented May 10, 2015 at 1:32
  • 1
    @Edwin - why don't you write your comment as an answer? Commented May 10, 2015 at 1:35

2 Answers 2

2

Turn the array into an object with the array members as keys, and values of anything (true) .

Then you can do "if (kind in obj)"

Also has the nice advantage of a O(1) lookup time instead of O(n)

Sign up to request clarification or add additional context in comments.

Comments

0

I've wrote this a long time ago, idk if this helps. (Rewritten in ES6 arrow functions are commented out)

Array.prototype.includes = function(element) {
    return this.some(function(el) {
        return (typeof el == "string") ? el.includes(element) : el === element;
    });
    // ES6 Arrow Functions
    // return this.some(el => (typeof el == "string") ? el.includes(element) : el === element);
}

String.prototype.containsAll = function(arr, index){
    return arr.every(function(el){
        return this.includes(el, index);
    }, this);
    // ES6 Arrow Functions
    // return arr.every(el => this.includes(el, index), this);
}

String.prototype.contains = function(arr, index) {
    return arr.some(function(el){
        return this.includes(el, index);
    }, this);
    // ES6 Arrow Functions
    // return arr.some(el => this.includes(el, index), this);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.