简体   繁体   English

如何使用AngularJS遍历对象的“键”值?

[英]How to loop through the 'key' value of an object with AngularJS?

I have the following data and HTML template (and other code in app.js obviously). 我有以下数据和HTML模板(显然还有app.js中的其他代码)。 The code in my "tbody" works perfectly, and displays a table like this: 我的“ tbody”中的代码可以完美运行,并显示如下表:

current output 电流输出

--------------------
| AAPL | 127 | 128 |
--------------------
| GOOG | 523 | 522 |
--------------------
| TWTR |  35 |  36 |
--------------------

Now I'm trying to loop through the 'keys' of the first object and display them in a 'thead' like so: 现在,我试图遍历第一个对象的“键”,并以“ thead”形式显示它们,如下所示:

desired output 期望的输出

--------------------
| Name | jan | feb |
--------------------
| AAPL | 127 | 128 |
--------------------
| GOOG | 523 | 522 |
--------------------
| TWTR |  35 |  36 |
--------------------

data 数据

$scope.data = [
{  
    "name": "AAPL",
    "jan": "127",
    "feb": "128"
},
{
    "name": "GOOG",
    "jan": "523",
    "feb": "522"
},
{
    "name": "TWTR",
    "jan": "35",
    "feb": "36"
}]

html HTML

<table>
    <thead>
        <tr>
            <td ng-repeat="">{{}}</td>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="object in data">
            <td ng-repeat="(a,b) in object">{{b}}</td>
        </tr>
    </tbody>
</table>

It has been pointed out that this question is a duplicate of another one, although, the other one is in plain JS, this one uses AngularJS. 有人指出,这个问题是另一个问题的重复,尽管另一个问题是纯JS,但是这个问题使用了AngularJS。

If you use underscoreJS (which is a very common and competent convenience library) then you can get the keys of the first object like this: 如果使用underscoreJS(这是一个非常常见且功能强大的便捷库),则可以像这样获得第一个对象的键

_.keys(data[0])

This presume that all elements in the array have the same keys & structure. 这假定数组中的所有元素都具有相同的键和结构。 Looks like it, so you should be safe. 看起来像,所以您应该很安全。

After including UnderscoreJS in your app, then you would call it from the template in the bracketed expression: 在您的应用中包含UnderscoreJS之后,您可以从方括号中的模板中调用它:

 <td ng-repeat="">{{ _.keys(data[0])}}</td>

Pure ES5 solution 纯ES5解决方案

If your hard-up against a library, and you can stomach ES5, then you can use 如果您遇到图书馆问题,并且可以忍受ES5,则可以使用

Object.keys(data[0])
<td ng-repeat="">{{ Object.keys(data[0]) }}</td>
<td ng-if="data.length" ng-repeat="(a,b) in data[0]">{{a}}</td>

您只需要考虑键值始终以相同的顺序出现,否则表将以错误的顺序显示值。

You use use Angular's (key, value) syntax. 您使用Angular的(key, value)语法。 However, I don't think this will work in your case because the order of iteration is random. 但是,我认为这不适用于您的情况,因为迭代顺序是随机的。

I think your best best is either to hardcode the keys in a $scope.headings variable or to update the format of your data (if you have control over how it is generated) so that the order of keys is specified. 我认为最好的办法是对$scope.headings变量中的键进行硬编码,或者更新数据的格式(如果可以控制其生成方式),以便指定键的顺序。

@murid, Do you need this: @murid,您需要这个吗:

In script tag of head: 在head的脚本标签中:

 var app = angular.module('myApp', []); app.controller('myCtrl', function ($scope) { $scope.data = [{ "name": "AAPL", "jan": "127", "feb": "128" }, { "name": "GOOG", "jan": "523", "feb": "522" }, { "name": "TWTR", "jan": "35", "feb": "36" }]; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="myCtrl"> <table> <thead> <tr> <td ng-repeat="">{{}}</td> </tr> </thead> <tbody> <tr ng-repeat="object in data"> <td ng-repeat="(a,b) in object">{{b}}</td> </tr> </tbody> </table> </div> 

Hope this help. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何循环 2 个 arrays 以创建新的键/值 object - How to loop through 2 arrays to create a new key/value object 如何在javascript中使用特定的键和值循环遍历json对象? - How to loop through json object with specific key and value in javascript? 遍历对象并将键和值推入数组 - Loop through an object and push key and value into an array 如何遍历 object 键的值,如果匹配,则将其与另一个值进行比较,而不是将其更改为另一个值 - How to loop through the value of a object key compare it to another value if match than change it to another value 是否可以遍历对象并获取键名($ key =&gt; $ value)? - Is it possible to loop through an object, and get the key names ($key => $value)? 如何在angularjs中按值分组对象键 - How to group object key by value in angularjs 如何从angularjs中的对象获取键值 - How to fetch the key value from object in angularjs 如何循环对象并将键值推入键值对象 - How to loop an object and push key value into a key-value object 如何遍历一个数组(递归)并将对象键/值对推入另一个数组中? - How do I loop through an array (recurision) and push object key/value pairs in another array? 如何遍历嵌套的JSON对象并在Javascript中保存多个键值对 - How to loop through a nested JSON object and save the multiple key-value pairs in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM