从对象计算状态并显示结果

Calculate statuses from the object and display the results

请告诉我如何计算对象的状态数并在 countStatus 中显示结果?

var objs = {
  "Name A": [
    {
      "id": 3,
      "status": "allow"
    },
    {
      "id": 5,
      "status": "new"
    },
    {
      "id": 8,
      "status": "new"
    }
  ],
  "Name B": [
    {
      "id": 7,
      "status": "new"
    }
  ]
}
<div ng-repeat="(key, list) in objs">
  {{key}}
  <div ng-repeat="item in list">
     {{item.status}} : {{countStatus}}
  </div>
</div>

预期结果:

Name A
allow : 1
new : 2

Name B
new : 1

您可以使用 Array.prototype.reduce() 方法。下面的代码显示了如何创建所需的结果。

const objs = {
  'Name A': [
    {
      id: 3,
      status: 'allow',
    },
    {
      id: 5,
      status: 'new',
    },
    {
      id: 8,
      status: 'new',
    },
  ],
  'Name B': [
    {
      id: 7,
      status: 'new',
    },
  ],
};

const ret = Object.keys(objs).map((x) => {
  return {
    [x]: Object.values(objs[x]).reduce((prev, c) => {
      const p = prev;
      p[c.status] = p[c.status] || 0;
      p[c.status] += 1;
      return p;
    }, {}),
  };
});
console.log(ret);

在 Angular 中像下面这样打印 ret:

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