最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Test if key-name exists in JavaScript object literal - Stack Overflow

programmeradmin2浏览0评论

In the text adventure I am making, my object literals for the rooms look like this:

room : {
  // some info,
  exits : {
    north : -1,
    east : "house",
    south : "forest",
    west : -1
  }
}

and in my function to move around it says:

if (room["exits"][direction] !== -1) {// go that way}
else {print "you can't go that way!"}

now I want to save space by just testing if the key for the relevant direction exits in the object. so the literals will go:

room : {
  // some info,
  exits : {
    east : "house",
    south : "forest"
  }
}

... what should my if statement look like? what is the 'proper' way to ascertain if a given key-name exits in the object?

In the text adventure I am making, my object literals for the rooms look like this:

room : {
  // some info,
  exits : {
    north : -1,
    east : "house",
    south : "forest",
    west : -1
  }
}

and in my function to move around it says:

if (room["exits"][direction] !== -1) {// go that way}
else {print "you can't go that way!"}

now I want to save space by just testing if the key for the relevant direction exits in the object. so the literals will go:

room : {
  // some info,
  exits : {
    east : "house",
    south : "forest"
  }
}

... what should my if statement look like? what is the 'proper' way to ascertain if a given key-name exits in the object?

Share Improve this question edited Feb 21, 2017 at 8:18 bluish 27.4k28 gold badges125 silver badges184 bronze badges asked Dec 22, 2013 at 1:36 drenldrenl 1,3434 gold badges19 silver badges32 bronze badges 5
  • These all seem like good answers. Here's a related question. What does a non-key value return? Ie: in the above example, var foo = room.exits[bar] ... is foo null? undefined? – drenl Commented Dec 22, 2013 at 1:50
  • room.exits[bar] will returned undefined if bar doesn't exist. 'bar' in room.exits will return false. – p.s.w.g Commented Dec 22, 2013 at 1:53
  • ok. Challenge Question: can you point me to a reference that explains null, undefined, and falsey (as they are applicable in this context), that a noob would have a chance of understanding ? – drenl Commented Dec 22, 2013 at 2:00
  • See stackoverflow./questions/5076944/… and sitepoint./javascript-truthy-falsy – p.s.w.g Commented Dec 22, 2013 at 2:33
  • excellent. thank you all for the discussion! – drenl Commented Dec 22, 2013 at 2:42
Add a ment  | 

3 Answers 3

Reset to default 6

You can use the in operator:

if (direction in room.exits) {
    // go that way
} else { 
    console.log("you can't go that way!");
}

If there's absolutely no chance that it'll be null, a blank string, zero, or any other 'falsey' JS value you can just do

if(room.exits[direction]) { // go that way }
else {print "you can't go that way!"}

I also did a speed test against the 'in' operator that p.s.w.g posted as an answer because it never really occurred to me to use it. I found some interesting results that you should consider if you're running this in any kind of loop at any point.

http://jsperf./test-in-operator-vs-if

It seems that the "in" operator is SIGNIFICANTLY slower on IE and Chrome, but on Firefox it's almost twice as fast.

you should do:

if (room.exits.south) {// go that way}
else {print "you can't go that way!"}

that's it.

When "south" is undefined (or when it is zero or empty string or literal false), the predicate evaluates to false.

发布评论

评论列表(0)

  1. 暂无评论