0

{
  "GE": {
    "symbol": "GE",
    "dxSymbol": "GE",
    "exchange": "XNYS",
    "isoExchange": "XNYS",
    "bzExchange": "NYSE",
    "type": "STOCK",
    "name": "General Electric",
    "description": "General Electric Company Common Stock",
    "sector": "Industrials",
    "industry": "Diversified Industrials",
    "open": 28.59,
    "high": 29.07,
    "low": 28.55,
    "close": 28.87,
    "bidPrice": 28.82,
    "askPrice": 28.94,
    "askSize": 7,
    "bidSize": 10,
    "size": 1676188,
    "bidTime": 1477526400000,
    "askTime": 1477526400000,
    "lastTradePrice": 28.87,
    "lastTradeTime": 1477512029000,
    "volume": 24357188,
    "change": 0.22,
    "changePercent": 0.77,
    "previousClosePrice": 28.65,
    "fiftyDayAveragePrice": 31.0652,
    "fiftyTwoWeekHigh": 33.0,
    "fiftyTwoWeekLow": 19.37,
    "marketCap": 291853729930,
    "sharesOutstanding": 10109239000,
    "pe": 36.964286,
    "forwardPE": 20.7039,
    "dividendYield": 2.96,
    "payoutRatio": 109.52,
    "ethPrice": 28.87,
    "ethVolume": 777095,
    "ethTime": 1477526274000
  }
}

In the above JSON .if I don't know child object is GE how can I get it using angular JS. I am getting this response as from get endpoint , I want to access direct child is that possible if i do not know it is "GE" on before hand

sravan kumar
  • 3
  • 1
  • 1
  • Welcome to SO! Not to be harsh, but please do search on google. Also break down your problem and try to search solution individually and they tie them up. This way, you will learn something new. Also, next time, do share your effort. If you are not sure where to start, then try to explain what you are trying to achieve and what is the approach you think is feasible. – Rajesh Oct 27 '16 at 06:30

2 Answers2

0

Object.keys(object) to get the array of keys. Then take the first one

var myObj = {
  "GE": {
    "symbol": "GE",
    "dxSymbol": "GE",
    "exchange": "XNYS",
    "isoExchange": "XNYS",
    "bzExchange": "NYSE",
    "type": "STOCK",
    "name": "General Electric",
    "description": "General Electric Company Common Stock",
    "sector": "Industrials",
    "industry": "Diversified Industrials",
    "open": 28.59,
    "high": 29.07,
    "low": 28.55,
    "close": 28.87,
    "bidPrice": 28.82,
    "askPrice": 28.94,
    "askSize": 7,
    "bidSize": 10,
    "size": 1676188,
    "bidTime": 1477526400000,
    "askTime": 1477526400000,
    "lastTradePrice": 28.87,
    "lastTradeTime": 1477512029000,
    "volume": 24357188,
    "change": 0.22,
    "changePercent": 0.77,
    "previousClosePrice": 28.65,
    "fiftyDayAveragePrice": 31.0652,
    "fiftyTwoWeekHigh": 33.0,
    "fiftyTwoWeekLow": 19.37,
    "marketCap": 291853729930,
    "sharesOutstanding": 10109239000,
    "pe": 36.964286,
    "forwardPE": 20.7039,
    "dividendYield": 2.96,
    "payoutRatio": 109.52,
    "ethPrice": 28.87,
    "ethVolume": 777095,
    "ethTime": 1477526274000
  }
};
var key = Object.keys(myObj)[0];
console.log(myObj[key]);
Weedoze
  • 13,683
  • 1
  • 33
  • 63
-1

Note: Before you ask a question, please check for sometime regarding this, if you didnot find any answers, then ask your questions.

Use,

Object.keys(hash) property.

so,

Object.keys(object)[0] will give you the value of first key

// Instantiate the app, the 'myApp' parameter must 
// match what is in ng-app
var myApp = angular.module('myApp', []);

// Create the controller, the 'ToddlerCtrl' parameter 
// must match an ng-controller directive
myApp.controller('ToddlerCtrl', function ($scope) {
  
  $scope.data = {
  "GE": {
    "symbol": "GE",
    "dxSymbol": "GE",
    "exchange": "XNYS",
    "isoExchange": "XNYS",
    "bzExchange": "NYSE",
    "type": "STOCK",
    "name": "General Electric",
    "description": "General Electric Company Common Stock",
    "sector": "Industrials",
    "industry": "Diversified Industrials",
    "open": 28.59,
    "high": 29.07,
    "low": 28.55,
    "close": 28.87,
    "bidPrice": 28.82,
    "askPrice": 28.94,
    "askSize": 7,
    "bidSize": 10,
    "size": 1676188,
    "bidTime": 1477526400000,
    "askTime": 1477526400000,
    "lastTradePrice": 28.87,
    "lastTradeTime": 1477512029000,
    "volume": 24357188,
    "change": 0.22,
    "changePercent": 0.77,
    "previousClosePrice": 28.65,
    "fiftyDayAveragePrice": 31.0652,
    "fiftyTwoWeekHigh": 33.0,
    "fiftyTwoWeekLow": 19.37,
    "marketCap": 291853729930,
    "sharesOutstanding": 10109239000,
    "pe": 36.964286,
    "forwardPE": 20.7039,
    "dividendYield": 2.96,
    "payoutRatio": 109.52,
    "ethPrice": 28.87,
    "ethVolume": 777095,
    "ethTime": 1477526274000
  }
}

alert(Object.keys($scope.data)[0])
  
  
});
<!DOCTYPE html>
<html ng-app="myApp">

  <head>
    <script data-require="angular.js@1.2.7" data-semver="1.2.7" src="http://code.angularjs.org/1.2.7/angular.js"></script>
    <link href="style.css" rel="stylesheet" />
    <script src="script.js"></script>
  </head>
  <body>
    
    <div ng-controller="ToddlerCtrl">

    </div>
  </body>

</html>

Please run this snippet

Sravan
  • 18,467
  • 3
  • 30
  • 54
  • Should you not mark it as duplicate? – Rajesh Oct 27 '16 at 06:20
  • this is not a big thing to answer, so answered directly. – Sravan Oct 27 '16 at 06:21
  • If its an obvious duplicate, mark it as duplicate. This is not facebook that we can share same post again and again – Rajesh Oct 27 '16 at 06:22
  • @Rajesh, did you downvoted it? – Sravan Oct 27 '16 at 06:23
  • Yes. I did. Its not a revenge vote but an enforcement that we should not answer obvious question that have already been asked and answered – Rajesh Oct 27 '16 at 06:25
  • Answers should be downvoted only of they are wrong. not if duplicates are answered. If that is the case both answers are duplicates. I am also giving an example with his data explaining it as a duplicate. Pls remocve the downvote – Sravan Oct 27 '16 at 06:26
  • @Rajesh, please check the answer now. and take back your downvote as it is not the wrong answer. – Sravan Oct 27 '16 at 06:29
  • @Sravan Kumar Kilaru, please check my answer, do you find anything wrong in it? – Sravan Oct 27 '16 at 06:31
  • @Sravan Kumar Kilaru ,I think you haven't checked this answer because it is downvoted. I also added a snippet. – Sravan Oct 27 '16 at 06:33
  • @Sravan solution is also right , it's just the first solution suits my problem more. thank you for helping – sravan kumar Oct 27 '16 at 06:34
  • My apologies but this is not the point of SO! SO is about helping people learn new things and in doing so, solve their problems. If you would have marked as duplicate, OP would have got exposure to `for..in` and `Object.keys`. There are answer who explain the benefits of certain approach. So answering will limit exposure not only to OP but to everyone who reads your answer. This is our portal, lets try to keep it clean. And I have downvoted both answers and if you delete answer, you will get back the reputation. Hope you get my point. Have a good day. – Rajesh Oct 27 '16 at 06:35
  • yes bro, you are correct, but we answered it as soon as the question is asked without searching if it is a duplicate. You said that I can mark it as duplicate. that is well and good. But you cannot downvote a correct answer even if it is a duplicate, the users go into a misconception about it. Its all up to you here after. :) – Sravan Oct 27 '16 at 06:47