Sravan Kumar Kilaru Sravan Kumar Kilaru - 1 month ago 9
AngularJS Question

In the below JSON .if I don't know child object ,how can I get it using angular JS?



{
"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

Answer

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

Comments