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.