Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I have the following data structure:

var settings = {
    notifications: [
        {
            'logout' : {
                icon: 'fa fa-sign-out',
                text: [
                    {heading: 'Logout'},
                    {body: 'You have unsaved data. Are you sure you want to log out?'},
                ]
            }
        },
        {
            'delete' : {
                icon: 'fa fa-times',
                text: [
                    {heading: 'Delete'},
                    {body: 'This operation can not be undone. Are you sure you want to delete?'},
                ]
            }
        },
    ],
};

How do I retrieve the value of logout.icon, when I don't know the logout object's position in the notification array?

Solutions for plain nested objects listed here give me undefined.

--- SOLUTION

Based on Yassine Moustarham's answer here is my reusable function:

function getProp(arr,key,prop)
{
    for(var i=0;i<arr.length;i++)
    {
        if(arr[i][key])
        {
            return arr[i][key][prop];
        }
    }
    return false;
};

var icon = getProp(settings.notifications, 'logout', 'icon');

Thanks to Felix Kling for the simplification suggestion.

share|improve this question

marked as duplicate by Aaron Digulla, Teemu, Steve H., Timur, Donal Sep 11 '14 at 18:57

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3  
Wouldn't it be more logical to call your key something which can be applied to all objects? notifications: [ { type: 'logout', icon: ..., text: ... }, { type: 'delete', ... } ]. That way you only need to loop through the type properties contained within each of your nootifications' objects. –  James Donnelly Sep 11 '14 at 15:09
    
Thanks for the suggestion and the link, it's not the same case, but very useful nonetheless. –  TheRebel Sep 11 '14 at 15:35

2 Answers 2

up vote 3 down vote accepted

here you go : if you are sure that they will definitly be a member of one of the notifications array elements called logout then this function will return his icon member

function getIcon()
{
    for(i=0;i<settings.notifications.length;i++)
    {
        if(typeof(settings.notifications[i].logout)!=='undefined')
        {
            return settings.notifications[i].logout.icon;
        }
    }
    return false;
}

undefined was writting wrong ! and thinks to Felix Kling you need to add typeof befor comparing with 'undefined'.

share|improve this answer
    
This is simple and does exactly what I need. Thanks! –  TheRebel Sep 11 '14 at 15:31
    
you are welcom! –  Yassine Moustarham Sep 11 '14 at 15:36
    
I wonder why this was accepted. settings.notifications[i].logout!=='undifined' is very wrong. It should either be ... !== undefined or typeof ... !== 'undefined'. And it could be simplified to just if (...) –  Felix Kling Sep 11 '14 at 15:47
1  
you're right my bad ;) @FelixKling i will update the answer –  Yassine Moustarham Sep 11 '14 at 15:53
1  
@TheRebel: Nope, but you could have pointed out the mistake ;) It just seems odd to have incorrect code in an accepted answer, even if it is a tiny mistake. –  Felix Kling Sep 11 '14 at 16:14

You'll need to iterate over the object's properties recursively until you find the key you're looking for:

function findKey(object, key){
  if (object.hasOwnProperty(key)) {
    // do stuff
    console.log('found');
    return true;
  }
  for (var property in object) {
    if (typeof object[property]=='object') {
      return findKey(object[property], key);
    } 
  }
}

findKey({'bop':{'foo':'bar'}}, 'foo');
share|improve this answer
    
won't this only return the value of 'foo' within the 'bop' object if there is such a key? what if there are multiple bop objects in the array? –  TheRebel Sep 11 '14 at 15:24
    
@TheRebel It's recursive. It calls itself and would return on the first time the key is found. –  Calvin Froedge Sep 11 '14 at 15:26
    
@TheRebel If you want it to handle multiple 'bop' objects, take out the return statements and it will iterate until it has looked at every object / key. –  Calvin Froedge Sep 11 '14 at 15:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.