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

jquery - Javascript - Check if key exists in object - Stack Overflow

programmeradmin2浏览0评论

I'm building a object in Javascript to parse the uri contents and attach their key / value pairs to it. However, I'm stuck on how to find out if a key exists. Here's the code :

var uri = {
    segments : {},
    parse : function() {
        var segments = {};
        var parts;
        var s;

        parts = location.href.split('/');
        parts = parts[3].split('?');
        parts = parts[1].split('&');

        for (var i = 0; i < parts.length; i++) {
            s = parts[i].split('=');
            segments[s[0]] = s[1];
        }

        uri.segments = segments;

        return segments;
    },
    segment : function(key) {
        if (uri.segments.length == 0)
        {
            uri.parse();
        }

        /* before was 'key in uri-segments' */      
        if (Object.prototype.hasOwnProperty.call(uri.segments, key))
        {
            return uri.segments[key];
        }
        else
        {
            return false
        }
    },
};

edit : full code

I'm building a object in Javascript to parse the uri contents and attach their key / value pairs to it. However, I'm stuck on how to find out if a key exists. Here's the code :

var uri = {
    segments : {},
    parse : function() {
        var segments = {};
        var parts;
        var s;

        parts = location.href.split('/');
        parts = parts[3].split('?');
        parts = parts[1].split('&');

        for (var i = 0; i < parts.length; i++) {
            s = parts[i].split('=');
            segments[s[0]] = s[1];
        }

        uri.segments = segments;

        return segments;
    },
    segment : function(key) {
        if (uri.segments.length == 0)
        {
            uri.parse();
        }

        /* before was 'key in uri-segments' */      
        if (Object.prototype.hasOwnProperty.call(uri.segments, key))
        {
            return uri.segments[key];
        }
        else
        {
            return false
        }
    },
};

edit : full code

Share Improve this question edited Mar 10, 2012 at 22:32 Dagg Nabbit 76.8k19 gold badges114 silver badges141 bronze badges asked Mar 10, 2012 at 22:09 yodayoda 11k19 gold badges67 silver badges93 bronze badges 10
  • What's wrong with using in? – user1106925 Commented Mar 10, 2012 at 22:13
  • 1 @amnotiam For example, imagine a key called toString. 'toString' in {} is true.` – Rob W Commented Mar 10, 2012 at 22:14
  • @RobW: By uri contents, I would assume that specific keys are used for the different parts. Unless perhaps it's going to separate out the querystring parameters into keys. – user1106925 Commented Mar 10, 2012 at 22:17
  • 1 @yoda The uri will never be parsed, because uri.segments.length == undefined (it is not an array). undefined == 0 is false. – Rob W Commented Mar 10, 2012 at 22:21
  • 1 @RobW Thank you, I failed to see that. Changed it to segments : [] and it worked :) – yoda Commented Mar 10, 2012 at 22:29
 |  Show 5 more ments

1 Answer 1

Reset to default 7

Use the hasOwnProperty method to check whether a key exists or not:

// hasOwnProperty from the objects prototype, to avoid conflicts
Object.prototype.hasOwnProperty.call(uri.segments, key);
//                                   ^ object      ^ key
发布评论

评论列表(0)

  1. 暂无评论