Accessing object properties "the php way"

Hey Sitepointers,

Apologies for a semi-misleading title, but i just didn’t know a better way to explain my problem.

Given the object:


// js object
var object = {
    id: 6,
    name: 'bob'
}

In php(if the above object were a php object) you are able to do something like this:


<?php
    $string = "name";
    echo $object->$string; // name
?>

My question is, i have a javascript function returns the string “name”. Is there a way i can use this value to access the object.name property.

In order to emulate the behaviour of the pseudo code below.


var property = (function() { return "name"; })(); // "name"
alert(object + '.' + property); // alerts "bob"

Note: I’m aware i can access object properties is associative array notation, however it is not uncommon for the object to have nested objects, in which case “var property” could be “nested_object_1.nested_object_2.name”

very nice wolken, much appreciated :slight_smile:


var url = "http://example.com/group/[id]/person/[person_id]";

var group_obj = {
    id: 10, // many other properties
    Person: {
        id: 15 // many other properties
    } // many nested objects are possible
}

// from the above variables i need to get the resulting value "http://example.com/group/10/person/15

var new_url = (function()
		{
			function replacer(str, p1, offset, s) 
			{
			// if the match is underscored it represents a nested object
			var parts = p1.split('_');
			
			var tmp = $.map(parts, function(item, i) {
				if(i < parts.length - 1) {
					return item.charAt(0).toUpperCase() + item.slice(1).toLowerCase();
				}
				return item;
			});
			
			var obj=group_obj;
			$.each(tmp,function(i,item)
			{obj=obj[item]});
			// return the associated value from the group_obj
			return obj;
    		}
			// find anything matching this template in url [underscored_value]
			// and call replacer on it.
			return url.replace(/\\[([a-z_]+)\\]/g, replacer);
		})();

Enjoy !

I have found, as i suspected but couldn’t manifest, that this is possible using eval.

alert(eval("obj." + property)); // alerts "name"

Although according to MDC this use is discouraged in favor of using the array notation. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/eval

So maybe it’s best i post my specific example and ask for suggestions. Below is a trivial example of what i need to accomplish and my current implementation:



var url = "http://example.com/group/[id]/person/[person_id]

var group_obj = {
    id: 10, // many other properties
    Person: {
        id: 15 // many other properties
    } // many nested objects are possible
}

// from the above variables i need to get the resulting value "http://example.com/group/10/person/15

var new_url = (function(){
    function replacer(str, p1, offset, s) {
        // if the match is underscored it represents a nested object
        // convert strings like ab_cd_ef to Ab.Cd.ef
        var parts = p1.split('_');
        var tmp = $.map(parts, function(item, i) {
            if(i < parts.length - 1) {
                return item.charAt(0).toUpperCase() + item.slice(1).toLowerCase();
            }
            return item;
        }).join('.');

        // return the associated value from the group_obj
        return eval("group_obj." + tmp);
    }

    // find anything matching this template in url [underscored_value]
    // and call replacer on it.
    return url.replace(/\\[([a-z_]+)\\]/g, replacer);
})();

What are the issues with the above implementation, and/or better ways to achieve what i’m after?

as simple as that :


var property = (function() { return "name"; })(); // "name"
alert(object[property]); // alerts "bob"

:slight_smile: