AngularJS: How to get the key of a JSON Object

后端 未结 4 1394
孤独总比滥情好
孤独总比滥情好 2021-01-31 14:45

I am unsure if this has got anything to do with AngularJS at all and if it is only JSON related.

Anyhow, let us say that we have the following JSON:

$s         


        
相关标签:
4条回答
  • 2021-01-31 14:52

    for accessing Json key-value pair from inside controller in AngularJs.

    for(var keyName in $scope.dataSets){        
         var key=keyName ;
         var value= $scope.dataSets[keyName ];
    
      alert(key)
      alert(JSON.stringify(value));
    
    }
    
    0 讨论(0)
  • 2021-01-31 15:05

    For every dataSet in dataSets, print the key and then iterate through the individual items:

    <div ng-repeat="(key, dataSet) in dataSets">
       <div>{{key}}</div>
       <div ng-repeat="value in dataSet">   
            {{value}}
       </div>
    </div>
    

    {{dataset}} can be displayed in one go also, the array would be displayed as a comma separated list of values.

    0 讨论(0)
  • 2021-01-31 15:07

    As docs state it:

    (key, value) in expression – where key and value can be any user defined identifiers, and expression is the scope expression giving the collection to enumerate.

    <div ng-repeat="(key, data) in dataSets">
      {{key}}
    </div>
    
    0 讨论(0)
  • 2021-01-31 15:11

    if dataSets is an array and not an object you first need to ng-repeat the array items and then the key or value.

    <div ng-repeat="item in dataSets">
       <div ng-repeat="(key, value) in item">
       {{key}}
       {{value}}
       </div>
    </div>
    

    just my 2 cents.

    0 讨论(0)
提交回复
热议问题