Solved

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

Posted on 2016-09-01
3
116 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 54

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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
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)
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.

786 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