基于相同键链接数组数据

link array data based on the same keys

本文关键字:数组 数据 链接 于相同      更新时间:2023-09-26

数组

[
  code: 'code',
  specName: [
              0: 'First',
              1: 'Second',
              2: 'Third'
            ],
  year: [
              0: '2011',
              1: '2012',
              2: '2013'
            ]
];

低谷

我正在使用 AngularJS 输出数据,使用 ng-repeat="name in module.specName" .我想做的是将year[0]specName[0]联系起来等等。所以我的输出将如下所示:

 -------------------
| specName |  year  |
 -------------------
|  First   |   2011 |
 -------------------
|  Second  |   2012 |
 -------------------
|  Third   |   2013 |
 -------------------

问题

我能否被指出如何实现这一目标的正确方向。我必须编写的角度.filter,或者在角度应用程序中重新构建一些数据或其他东西。

<div ng-repeat="(key,value) in data.specName">
    <span>{{value}}</span>
    <span>{{data.year[key]}}</span>         
</div>

.JS:

$scope.data={
    code: 'code',
    specName: {
        0: 'First',
        1: 'Second',
        2: 'Third'
    },
    year: {
        0: '2011',
        1: '2012',
        2: '2013'
    }
};

如果您能够像这样重组数据:

$scope.data = {
  code: 'code',
  specs: [
     {name: 'First', year:2011 },
     {name: 'Second', year:2012 },
     {name: 'Third', year:2013 }
  ]
};

因此,您将能够非常轻松地显示它:

 <table>
     <tr ng-repeat="spec in data.specs">
        <td>{{spec.name}}</td>
        <td>{{spec.year}}</td>  
      </tr>
 </table>

工作示例:http://plnkr.co/edit/Ka8jM8?p=preview