Get values by property name from an object at different levels

Viewed 1272

I have an object in the below format and I need to get all values from the Price property at all levels of the object.

var o = {
    Id: 1,
    Price: 10,
    Attribute: {
        Id: 1,
        Price: 2,
        Modifier: {
            Id: 34,
            Price: 33
        }
    }
};

I was thinking of LinqToJS and jquery.map() methods but I'd like to get a method as generic as possible. I tried this but it only works at the first level:

var keys = $.map(o, function(value, key) {
    if (key == "Price") {
        return value;
    }
});
3 Answers
Related