javascript - 在AngularJS中按两级分层组织对象

标签 javascript arrays angularjs multidimensional-array

我正在开发一个 Angular 应用程序,它使用 cell modifier里面 custom cell template在 Angular Bootstrap 日历中。在每个单元格内,我放置了一组 table ,而不是标准事件,用于报名当天车站的轮类。这些表分为两组;上午和下午,在每个上午/下午组内,每个电台都有一个表,每个表三行。

上午

| 1 |职位|姓名|
| |职位 1 |姓名 1|
| |位置 2 |姓名 2|
| |位置 3 |姓名 3|

| 2 |职位|姓名|
| |职位 1 |姓名 1|
| |位置 2 |姓名 2|
| |位置 3 |姓名 3|

下午

| 1 |职位|姓名|
| |职位 1 |姓名 1|
| |位置 2 |姓名 2|
| |位置 3 |姓名 3|

| 2 |职位|姓名|
| |职位 1 |姓名 1|
| |位置 2 |姓名 2|
| |位置 3 |姓名 3|

在我的单元修改器函数中,我得到了当天的类次对象数组,每个类次对象都包含 ampm 值和电台值:

 {
    "_id": "57776537ac0a88010063b9b9",
    "modified": "2016-07-02T06:54:47.518Z",
    "data": {
      "station": "1",
      "date": "2016-07-01T07:00:00.000Z",
      "ampm": "pm",
      "slots": [
        {
          "position": "AO",
          "name": ""
        },
        {
          "position": "FF",
          "name": {
            "_id": "57776507ac0a88010063b9b8",
            "modified": "2016-07-02T06:53:59.661Z",
            "data": {
              "group": "suppression",
              "driving": {
                "n": false,
                "d": true,
                "ao": false,
                "wt": false
              },
              "emtLevel": "b",
              "secondaryPhoneNumber": "",
              "primaryPhoneNumber": "5556781234",
              "emailAddress": "person.one@mysite.com",
              "fullName": "Person One",
              "userName": "person.one",
              "assignedStation": "18",
              "probationary": false
            },
            "form": "57427ba554ec330100dad645",
            "created": "2016-07-02T06:53:59.644Z",
            "externalIds": [],
            "access": [],
            "roles": [
              "573511a8ffaa7a0100a5718a"
            ],
            "owner": "57776507ac0a88010063b9b8"
          }
        },
        {
          "position": "FF",
          "name": {
            "_id": "57439d856e67b40100d4c420",
            "modified": "2016-05-24T00:17:09.493Z",
            "data": {
              "userName": "person.two",
              "fullName": "Person Two",
              "emailAddress": "person.two@mysite.com",
              "primaryPhoneNumber": "5555556666",
              "secondaryPhoneNumber": "",
              "assignedStation": "",
              "emtLevel": "b",
              "driving": {
                "d": true
              },
              "group": "suppression"
            },
            "form": "57427ba554ec330100dad645",
            "created": "2016-05-24T00:17:09.474Z",
            "externalIds": [],
            "access": [],
            "roles": [
              "573511a8ffaa7a0100a5718a"
            ],
            "owner": "5734bba2ffaa7a0100a57029"
          }
        }
      ]
    },

所以问题是如何获取这些对象并将它们组织到上面提到的两个分组中,以便我可以在模板中使用 ngRepeat 循环遍历它们。到目前为止我所拥有的是:

vm.cellModifier = function(cell) {
  cell.text = 'Test Text';
  var shifts = vm.events;
  // Get the date for the cell.
  this.cellDate = moment(cell.date).format('YYYY-MM-DD');
 // Iterate over shifts to get ones for this day.
  this.cell = cell;
  this.todayShifts = {};
  shifts.forEach(function(shift, index, allShifts) {
    var shiftDate = moment(shift.data.date).format('YYYY-MM-DD');
    // Now we need to see if this shift belongs to this day.
    if (moment(vm.cellDate).isSame(moment(shiftDate))) {
      // Shift is today, so let's put it into the appropriate array.
      if (typeof vm.todayShifts[shift.data.ampm] == 'undefined') {
        vm.todayShifts[shift.data.ampm] = shift;
      } else {
        vm.todayShifts[shift.data.ampm].push(shift);
      }
    }
  });
  // Add arrays to cell object.
  cell.todayShifts = vm.todayShifts;
};

这给出了 vm.todayShifts[am]vm.todayShifts[pm],但我还想获得第二个级别,以便我有 vm.todayShifts[am][1]vm.todayShifts[am][2] 等。有没有更简单的方法来完成我想做的事情(我相当确定有)而不是添加另一段语句?我想知道自定义指令或组件是否可能更干净,因为这样我就可以将数据传递到该 Controller 中,但即便如此,我仍然需要正确排列我的数据,以便它可以按正确的顺序显示。

希望这一切都有道理。

谢谢。

最佳答案

我认为你实际上非常接近...尝试这个,它总是以你要求的格式放置数据。

shifts.forEach(function(shift, index, allShifts) {
  var shiftDate = moment(shift.data.date).format('YYYY-MM-DD');
  // Now we need to see if this shift belongs to this day.
  if (moment(vm.cellDate).isSame(moment(shiftDate))) {
    // Shift is today, so let's put it into the appropriate array.
    if (typeof vm.todayShifts[shift.data.ampm] == 'undefined') {

      // Initialize the shifts as an array.
      vm.todayShifts[shift.data.ampm] = [];
    }

    // Push the shift onto the array.
    vm.todayShifts[shift.data.ampm].push(shift);
  }
});

关于javascript - 在AngularJS中按两级分层组织对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38193823/

相关文章:

javascript - 将视频从phonegap上传到php

javascript - 使用javascript替换特定位置的所有空格

javascript - 如何从 flickr API 获取正确的 JSON 对象

javascript - 通过在javascript中传递键和值从数组中获取对象

php - 如何实现我的算法文本更正以替换文本中的单词?

javascript - GAS/Javascript 在汇总数字时给出错误的数字

javascript - Angular js错误: Argument 'questionsCtrl' is not a function

javascript - 如何延迟发出错误的 Observable

javascript - 将字符串表转换为具有范围的 int

javascript - Bootstrap 分页不适用于 angular-ui-bootstrap/2.5.0/ui-bootstrap-tpls.js