Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2016-09-01
3
126 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 22

Assisted Solution

by:Kim Walker
Kim Walker earned 250 total points
ID: 41780991
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 55

Accepted Solution

by:
Julian Hansen earned 250 total points
ID: 41781359
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
ID: 41781553
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With the rapid rise in mobile usage, mobile devices are here to stay and have become an integral part of doing business. Here are 9 great apps for your BYOD environment.
Ransomware is a growing menace to anyone using a computer or mobile device. Here are answers to some common questions about this vicious new form of malware.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

856 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