Solved

Get specific item from object in javascript without knowing the name of the object inside my object?

Posted on 2016-09-01
3
100 Views
Last Modified: 2016-09-02
I would like to get the value of "name" from the example json object below without knowing the name of the object (123) because this could be anything and I dont know before.

{
  "123": {
    "email": "kato@firebase.com",
    "name": "Kato"
  }
}

Open in new window


I know it dont work like this but I look for something like
console.log(object[0].name);

to make it more clear.
0
Comment
Question by:Oliver2000
3 Comments
 
LVL 21

Assisted Solution

by:Kim Walker
Kim Walker earned 250 total points
Comment Utility
Is there always only one child object in the JSON object? If so, you could use
object = {
  "123": {
    "email": "kato@firebase.com",
    "name": "Kato"
  }
};
for (var i in object) {
  console.log(object[i].name);
}

Open in new window

0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 250 total points
Comment Utility
To get the first (or Nth if you desire) object from an object you can use Object.keys()

In your case for the first object use index 0
var data = {
  "123": {
    "email": "kato@firebase.com",
    "name": "Kato"
  }
};
console.log(data[Object.keys(data)[0]].name);

Open in new window

0
 

Author Closing Comment

by:Oliver2000
Comment Utility
Thank you guys, this was exactly what I needed.
Both solutions work fine for me but the second solution is best for me since I have some cases with more than   1 result.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
By this time the large percentage of day-to-day transactions have shifted to mobile banking; here are some overriding areas QAs must investigate while testing mobile banking apps.  
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now