如何以角度获取/显示数组对象的总长度

时间:2019-12-04 02:35:52

标签: javascript angular typescript

如何获取/显示lengthPENDING中的REJECTED的总数

如果status === 'PENDING',否则,如果status === 'REJECTED

代码如下:

list.components.ts

const group: any = groupBy(params, 'status');

    Object.values(group).map((x: any) => {
      x.map((key: any) => {
        console.log(key);
      });

这是array

{
          "assetcode": "001", "assetname": "Machine Print", "assettype": "Type", "floorcode": "58",
          "linecode": "02", "rowNo": 0, "lastpmdate": "2019-12-02 08:00:00", "status": "PENDING"
        },
        {
          "assetcode": "002", "assetname": "Machine Print", "assettype": "Type", "floorcode": "58",
          "linecode": "02", "rowNo": 0, "lastpmdate": "2019-12-02 08:00:00", "status": "PENDING"
        },
        {
          "assetcode": "003", "assetname": "Machine Print-V2", "assettype": "Type", "floorcode": "58",
          "linecode": "03", "rowNo": 0, "lastpmdate": "2019-12-02 08:00:00", "status": "PENDING"
        },
        {
          "assetcode": "003", "assetname": "Machine Print-V2", "assettype": "Type", "floorcode": "58",
          "linecode": "03", "rowNo": 0, "lastpmdate": "2019-12-02 08:00:00", "status": "PENDING"
        },
        {
          "assetcode": "003", "assetname": "Machine Print-V2", "assettype": "Type", "floorcode": "58",
          "linecode": "03", "rowNo": 0, "lastpmdate": "2019-12-02 08:00:00", "status": "PENDING"
        },
        {
          "assetcode": "003", "assetname": "Machine Print-V2", "assettype": "Type", "floorcode": "58",
          "linecode": "03", "rowNo": 0, "lastpmdate": "2019-12-02 08:00:00", "status": "PENDING"
        },
        {
          "assetcode": "003", "assetname": "Machine Print-V2", "assettype": "Type", "floorcode": "58",
          "linecode": "03", "rowNo": 0, "lastpmdate": "2019-12-02 08:00:00", "status": "REJECTED"
        },
        {
          "assetcode": "003", "assetname": "Machine Print-V2", "assettype": "Type", "floorcode": "58",
          "linecode": "03", "rowNo": 0, "lastpmdate": "2019-12-04 08:00:00", "status": "REJECTED"
        },
        {
          "assetcode": "003", "assetname": "Machine Print-V2", "assettype": "Type", "floorcode": "58",
          "linecode": "03", "rowNo": 0, "lastpmdate": "2019-12-03 08:00:00", "status": "PENDING"
        },

这是group

{PENDING: Array(6), REJECTED: Array(1)}
PENDING: Array(6)
0: {assetcode: "001", assetname: "Machine Print", assettype: "Type", floorcode: "58", linecode: "02", …}
1: {assetcode: "002", assetname: "Machine Print", assettype: "Type", floorcode: "58", linecode: "02", …}
2: {assetcode: "003", assetname: "Machine Print-V2", assettype: "Type", floorcode: "58", linecode: "03", …}
3: {assetcode: "003", assetname: "Machine Print-V2", assettype: "Type", floorcode: "58", linecode: "03", …}
4: {assetcode: "003", assetname: "Machine Print-V2", assettype: "Type", floorcode: "58", linecode: "03", …}
5: {assetcode: "003", assetname: "Machine Print-V2", assettype: "Type", floorcode: "58", linecode: "03", …}
length: 6
REJECTED: Array(1)
0: {assetcode: "003", assetname: "Machine Print-V2", assettype: "Type", floorcode: "58", linecode: "03", …}
length: 1
{PENDING: Array(2)}
PENDING: Array(2)
0: {assetcode: "003", assetname: "Machine Print-V2", assettype: "Type", floorcode: "58", linecode: "03", …}
1: {assetcode: "003", assetname: "Machine Print-V2", assettype: "Type", floorcode: "58", linecode: "03", …}
length: 2;
{REJECTED: Array(1)}
REJECTED: Array(1)
0: {assetcode: "003", assetname: "Machine Print-V2", assettype: "Type", floorcode: "58", linecode: "03", …}
length: 1

控制台 enter image description here

length10 8PENDING2REJECTED

应该这样显示

 if (key.status === 'PENDING') {
     /// the output should be 8
    } else if (key.status === 'REJECTED') {
 /// the output should be 2
}

1 个答案:

答案 0 :(得分:2)

假设group是附带屏幕截图的那个数组:

const pending = group.filter(asset => asset.status === 'PENDING').length;
const rejected = group.filter(asset => asset.status === 'REJECTED').length;

filter按状态进行过滤,length返回已过滤项目的总数。

如果您有多个数组,则可以先使用concat()将它们连接起来以获得单个数组:

var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var children = hege.concat(stale);

https://www.w3schools.com/jsref/jsref_concat_array.asp

注意:

在您的原始代码中,您正在做groupBy()用两个键(待定和拒绝)创建一个新对象,但是您根本不需要这样做,因为filter可以满足您的要求,使用concat可以将多个数组合并为一个。