www.webdeveloper.com
Results 1 to 5 of 5

Thread: Getting the value of a variable object key?

  1. #1
    Join Date
    Mar 2010
    Location
    Finland
    Posts
    77

    Getting the value of a variable object key?

    I have a snippet form a wikipedia api object and would like to know how to get the key "3823": which varies each time a different page is requested.

    Code:
    var wp = {
    	"query":{
    		"pages":{
    			"3823":{
    				"pageid":3823,
    				"ns":0,
    				"title":"Binary",
    				"extract":"<p><b>Binary</b> means <i>composed of two pieces or two parts</i> and may refer to:</p>\n\n",
    				"links":[{
    					"ns":0,"title":"Binary-coded decimal"},{
    					"ns":0,"title":"Binary (Assemblage 23 song)"},{
    					"ns":0,"title":"Binary code"}],
    				"langlinks":[{
    					"lang":"de","*":"Bin\u00e4r"},{
    					"lang":"fr","*":"Binaire"},{
    					"lang":"ur","*":"\u062a\u062b\u0646\u06cc\u06c1"}]
    			}
    		}
    	}
    };
    Thanks.

  2. #2
    Join Date
    Jul 2013
    Posts
    66
    The best thing would be to use a different data scheme. If you have no influence, I'm afraid it doesn't get any better than this:

    Code:
    var obj = wp["query"]["pages"];
    var key;
    for (var prop in obj) {
        if (obj.hasOwnProperty(prop)) {
            key = prop;
            break;
        }
    }
    
    alert(key);
    … unless you can use Javascript 1.8.5, then it can be written as

    Code:
    alert(Object.keys(wp["query"]["pages"])[0]);
    Both solutions assume there is only one key in the pages object.

  3. #3
    Join Date
    Mar 2009
    Posts
    430
    I'm a little unclear about what you really want. Is it just to find out if a given object has a key with a specific name nested somewhere within it (objects within objects), or is it to return the value of any key(s) within an object that have a given name? This can be done, but just what is it that you want?

  4. #4
    Join Date
    Mar 2010
    Location
    Finland
    Posts
    77
    Hi, It's exactly what Airblader thought. I wanted the value (key) of the "pages": object.
    Edit: I've just noticed the thread has a resolved option. I should probably use it :P
    Last edited by leke; 07-30-2013 at 01:10 AM.

  5. #5
    Join Date
    Mar 2009
    Posts
    430
    Here's something you might want. No matter how deeply nested an object within an object is, whether in a parent object or an array, this will find the values of any key that matches the second argument and will return an array containing those values. It always returns an array (which may be empty).

    Code:
    function finder(obj, key){
        var stack, result, i, j, len, isArr, done, trueType, beenThere, tmp, type, cat;
        //first get a way to distinguish between arrays and objects
        isArr = '  12qwe';  //random string
        if(!Array[isArr]){   // attach property to all arrays
    	Array.prototype[isArr] = true;
        }
        trueType = function(inp){  
    	var res = typeof(inp);
    	if(res != 'object'){
    	    return res;
    	}
    	if(inp === null){
    	    return 'null';
    	}
    	if(inp[isArr]){
    	    return 'array';
    	}
    	return res;
        }
    	
        stack = []; done = []; result= [];
        //if obj isn't an array or object don't process it
        type = trueType(obj);
        if(type != 'object' && type != 'array'){
    	return result;
        }
        // function to check to avoid exploring circular references
        beenThere = function(ref){
    	if(done.indexOf(ref) !== -1){
    	    return true;
    	}
    	done.push(ref);
    	return false;
        }
    
        stack.push(obj);
    
        while(stack.length > 0){
    	tmp = stack.shift();
    	if(beenThere(tmp)){  //if we've explored it once don't do it again--avoid endless loops
    	    continue;
    	}
    	type = trueType(tmp);
    	if(type == 'array'){  //objects might be inside arrays--search them
    	    len = tmp.length;
    	    for (j = 0; j < len; j++){
    		cat = trueType(tmp[j]);
    		if(cat == 'array' || cat == 'object'){
    		    stack.push(tmp[j]);
    		}
    	    }
    	}
    	else if(type == 'object'){ //its an object-object
    	    for(i in tmp){
    		if( i == key){  //is this what we are looking for?
    		    result.push(tmp[i]);  //if so, put value in result array
    		    break;
    		}
    		cat = trueType(tmp[i]);
    		if(cat == 'array' || cat == 'object'){
    		    stack.push(tmp[i]);
    		}
    	    }
    	}
        }
        return result;
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles