1

I've got many objects that structures aren't the same. But I know that all of them have got property name 'siteName'. My question is how can I get value from this property. Explame of few objects:

feature1 = {
    display: "name",
    feature: {
        attributes: {
            when: '111',
            what: '222'
        },
        geometry: null
        infoTemplate: undefined
    },
    symbol: null
    siteName: 'aa'
}

feature2 = {
    feature: {
        attributes: {
            when: '111',
            what: '222'
        },
        geometry: null
        infoTemplate: undefined
    },
    static: {
        format: {
            weight: 12,
            siteName: 'cccc'
        },
    }
}
4
  • possible duplicate of Dynamic object property name Commented Dec 3, 2012 at 14:00
  • @Esailija: I don't think that's a dupe. I think the point is that the location of the property in the nested structure is not predictable. Commented Dec 3, 2012 at 14:01
  • @Esailija it isn't duplicate - I want to find property in object by name Commented Dec 3, 2012 at 14:02
  • i think i should iterate trought the object - to find this property, but I dont know how to do it. Commented Dec 3, 2012 at 14:04

3 Answers 3

3

Here's a recursive function that should work for you.

It returns the value of the first property found with the name, otherwise returns undefined.

function findByName(obj, prop) {
    for (var p in obj) {
        if (p === prop) {
            return obj[p];
        } else if (obj[p] && typeof obj[p] === "object") {
            var result = findByName(obj[p], prop);
            if (result !== undefined)
                return result;
        }
    }
}

var result = findByName(myObject, "siteName");

Or here's another variation that avoids inherited properties.

function findByName(obj, prop) {
    if (obj.hasOwnProperty(prop))
        return obj[prop];

    for (var p in obj) {
        if (obj[p] && typeof obj[p] === "object") {
            var result = findByName(obj[p], prop);
            if (result !== undefined)
                return result;
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Oh the cruel world of Stackoverflow. Beaten you to a answer by 8 seconds, but too slow to properly implement the recursiveness... Well played.
Beautiful function. I think I might have to use this later...and cerbrus...I was just working on the same thing when I found out he had posted this already...sucks to be on a laptop keyboard.
@Cerbrus: Yeah I saw your answer there... figured you'd have it updated pretty soon since you were so close. But FWIW, you don't need the toLowerCase(). You'll always get a lowercase "object" from typeof for native objects. :)
That was exactly the thing that was costing me time. For some reason, I typed typeof obj[k] == "Object", and that took me too long to debug :-/ @Christopher: shall we upvote each other so we can try to compete with a accepted answer? :P
@Cerbrus, I never even finished getting the answer complete. Maybe next time I'll type faster.
1

Recursively loop through the objects:

function find(obj, name) {
    for (var k in obj) { // Loop through all properties of the object.
        if(k == name){ // If the property is the one you're looking for.
            return obj[k]; // Return it.
        }else if (typeof obj[k] == "object"){ // Else, if the object at [key] is a object,
            var t = find(obj[k], name); // Loop through it.
            if(t){ // If the recursive function did return something.
                return t; // Return it to the higher recursion iteration, or to the first function call.
            }
        }               
    }
}

Usage:

find(feature1, "siteName"); //Returns "aa"

Comments

0

The following function should suit your needs:

function getFirstFoundPropertyValue(searchedKey, object) {
    if(typeof object === "object") {
        for (var key in object) {
            var currentValue = object[key];
            if(key === searchedKey) {
                return currentValue;
            }
            var nested = getFirstFoundPropertyValue(searchedKey, currentValue);
            if(typeof nested !== "undefined") {
                return nested;
            }
        }
    }
}

It returns the value of the key if the key is found, undefined otherwise. If the key appears several times, the first found one will be returned.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.