Thursday, April 23, 2015

JavaScript Object.keys

JavaScript has a useful little function called Object.keys(object) where you pass an object and get the "keys" (properties) of that object.  Once you have those, you can enumerate the values of those keys on the object you want.  Check it out:

function ListKeyValues(input) {
    var keys = Object.keys(input);
    
    for(var i = keys.length; --i >= 0;){
        console.log(input[keys[i]]);
    }
}

This is obviously a contrived example, but here's the usage:


ListKeyValues({Name: "John Doe", Age: 32});

No comments:

Post a Comment