Angular 排序 Table 嵌套对象数组

Angular Sorting Table Nasted Array of Objects

尝试找到按 属性 值排序此数组的最佳方法。

"data": [
    {
      "id": "1913209a-4b96-4e9f-9377-17cc86cd4465",
      "data": [
        {
          "name": "mid_serial",
          "data": {
            "editable": false,
            "value": "5.65.2.88 - 12338456",
            "required": false,
            "type": "text"
          }
        },
        {
          "name": "status",
          "data": {
            "editable": false,
            "value": "warning",
            "required": false,
            "type": "text"
          }
        },
        ...
      ]
    },
    {
      "id": "1913209a-4b96-4e9f-9377-17cc86cd4465",
      "data": [
        {
          "name": "mid_serial",
          "data": {
            "editable": false,
            "value": "5.65.2.88 - 12338456",
            "required": false,
            "type": "text"
          }
        },
        {
          "name": "status",
          "data": {
            "editable": false,
            "value": "warning",
            "required": false,
            "type": "text"
          }
        },
        ...
      ]
    }
    ...        
  ]

所以我将传递 "name" 属性 并且数组必须按相应的 "value" 属性 值排序。 (也可以反过来,不过很简单)

提前致谢

您可以使用 Javascript 排序功能添加自定义比较器。代码将是:

$scope.name = 'status' // Example

data.sort(function(a, b) {
   var aValue, bValue;
   a.data.forEach(function(element) { // Search the value
       if ($scope.name == element.name) aValue = element.data.value;
   }
   b.data.forEach(function(element) { // Search the value
       if ($scope.name == element.name) bValue = element.data.value;
   }
   return aValue - bValue; // Compare them in order to order the array
})

试一试告诉我一些事情,我现在无法测试。