11

There is a method in PHP called extract which does exactly what I want to do here. Say I have an object that looks like this:

var data = {
    name: "Olly"
    age: 19
};

I want to run a method like extract(data) so that I can then access the properties in that object by just using name and age, instead of data.name and data.age.

I've done a bit of Googling and couldn't find anything.

3
  • Are you using a minifier or optimizer like Yahoo YUI or Google Closure? Commented Jan 4, 2012 at 16:33
  • 1
    I suppose Javascript's with statement is more closely similar to PHP's extract method. That being said, with is not very common and I would not use it unless you truly understand how it works. See developer.mozilla.org/en/JavaScript/Reference/Statements/with Commented Jan 4, 2012 at 16:36
  • @KrisKrause No, I am not. Why do you ask? Commented Jan 4, 2012 at 16:36

2 Answers 2

10

You can use something like this:

function myTestFunction() {
    var data = {
        name: "Olly",
        age: 19,
    };
    for (var key in data) {
        this[key] = data[key];
    }
    alert(name +" is "+ age +"!");
}
myTestFunction();

(Try that here: http://jsfiddle.net/dHDxd/3/)

Or even export them to global namespace, by using window[key] = data[key]. In any case, be very very careful with that, since the risk of clobbering the global namespace / override other stuff / etc. is very high.

Update: general-purpose extract()

function extract(data, where) {
    for (var key in data) {
        where[key] = data[key];
    }
}

function runTest() {
    var myData = { name: "Olly", age: 19, };
    extract(myData, this);
    alert(name +" is "+ age +"!");
}

runTest();
Sign up to request clarification or add additional context in comments.

5 Comments

Updated to fix naming: it's called extract(), not export() :)
By saying this[key] = data[key] in a function aren't you already clobbering the global object? Also note that this will be undefined in strict mode in that function, and will therefor throw an error
Yep, if you're running that from a function in the global namespace, you're right.. It would be better to run that from an object instance method or so, or not use it at all; but since he asked "how to do this.."
Well, +1, but passing in a context like that is corny, isn't it? Why not keep the this like you had, and then call the function with call, and specify the this value that way?
it will just add the variables to global object (window)
1

Here is more generic/multi-level/recurrent namespace extractor :)

function extract(rootObject, key) {
    var parts = key.split('.');
    var currentKey = parts.shift();
    return parts.length > 0 ? extract(rootObject[currentKey], parts.join('.')) : rootObject[currentKey];
}

var myObject = { a: { b: { c: 7 }}};
console.log(extract(myObject, 'a.b.c'));
console.log(extract(myObject, 'a'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.