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'
        },
    }
}
roryf
  • 29,592
  • 16
  • 81
  • 103
Krystian
  • 458
  • 1
  • 9
  • 26
  • possible duplicate of [Dynamic object property name](http://stackoverflow.com/questions/4244896/dynamic-object-property-name) – Esailija Dec 03 '12 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. – I Hate Lazy Dec 03 '12 at 14:01
  • @Esailija it isn't duplicate - I want to find property in object by name – Krystian Dec 03 '12 at 14:02
  • i think i should iterate trought the object - to find this property, but I dont know how to do it. – Krystian Dec 03 '12 at 14:04

3 Answers3

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;
        }
    }
}
I Hate Lazy
  • 47,415
  • 13
  • 86
  • 77
  • 1
    Oh the cruel world of Stackoverflow. Beaten you to a answer by 8 seconds, but too slow to properly implement the recursiveness... Well played. – Cerbrus Dec 03 '12 at 14:26
  • 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. – SomeShinyObject Dec 03 '12 at 14:29
  • @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. :) – I Hate Lazy Dec 03 '12 at 14:31
  • 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 Dec 03 '12 at 14:34
  • @Cerbrus, I never even finished getting the answer complete. Maybe next time I'll type faster. – SomeShinyObject Dec 05 '12 at 00:32
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"
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
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.

sp00m
  • 47,968
  • 31
  • 142
  • 252