简体   繁体   English

如何根据Typecript中的某个给定数字对对象数组的元素进行分组

[英]How to group elements of an array of objects based on some given number in Typecript

I've a requirement in Angular project.我在 Angular 项目中有一个要求。 I've an array of objects.我有一个对象数组。 It contains 6 objects.它包含 6 个对象。 I want to group first 3 in one and other 3 in other group.我想将前 3 个分组,其他 3 个分组。 The array is staticKpi .该数组是staticKpi Here is my logic:这是我的逻辑:

  staticKpi=[{},{},{},{},{},{}];
  ...
  createGrouping() {
    var chunks = [],
      i = 0,
      n = this.staticKpi.length;
    while (i < n) {
      chunks.push(this.staticKpi.slice(i, (i += 3)));
    }
    this.staticKpi = chunks;
  }

Final array should be like:最终数组应该是这样的:

staticKpi=[[{},{},{}],[{},{},{}]]

But I'm not getting correct output.但我没有得到正确的输出。 Here's the stackblitz .这是stackblitz Please correct my mistake.请纠正我的错误。

it should help you:它应该可以帮助您:

const foo = [1, 2, 3, 4, 5, 6, 7, 8, 9]

const count = 3;
const len = foo.length / 3;

function splitInto(arr){
   return Array(len).fill(0).reduce((acc, _, index) => [...acc, arr.slice(index * 3, index * 3 + 3)], [])
}


const result = splitInto(foo) //  [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

I did not take into account arrays which length is not divided by 3. But it is not hard to figure out how to handle it我没有考虑长度不除以 3 的数组。但不难弄清楚如何处理它

function createGrouping(arrData) {
    let result = [];
    let arr3 = [];
    arrData.forEach(item => {
        if(arr3.length < 3) {
            arr3.push(item);
        } else if(arr3.length === 3) {
            result.push([...arr3]);
            arr3 = [];
        }
    });
    result = arr3.length > 0 ? result.push([...arr3]) : result;
    return result;
}

According to your stackblitz sample根据你的stackblitz样本

import { Component, OnInit, VERSION } from "@angular/core";

@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent implements OnInit {
  name = "Angular " + VERSION.major;

  staticKpi2: {
    kpi: string;
    headerString: string;
    footerString: string;
  }[] = [];

  staticKpi: {
    kpi: string;
    headerString: string;
    footerString: string;
  }[][] = [];

  breakPoint = 3;

  ngOnInit() {
    this.populateObject();
  }

  populateObject() {
    this.staticKpi2.push(
      ...[
        {
          kpi: "SpO2",
          headerString: "High: 97 and above",
          footerString: "Low: 94 and below"
        },
        {
          kpi: "Temperature",
          headerString: "High: 100.4 and above",
          footerString: "Low: 94 and below"
        },
        {
          kpi: "BP",
          headerString: "High: 140/90 ",
          footerString: "Low: 90/60"
        },
        {
          kpi: "Respiratoin",
          headerString: "High: 25 per min",
          footerString: "Low: 10 per min"
        },
        {
          kpi: "Pulse rate",
          headerString: "High: 100 and above",
          footerString: "Low: 50 and below"
        },
        {
          kpi: "D-Dimer",
          headerString: "Negative: 0.50 and Less",
          footerString: "Positive: Greater than 0.50"
        }
      ]
    );
    // console.log(this.staticKpi);
    this.createGrouping();
  }

  createGrouping() {
    for (let i = 0; i < this.staticKpi2.length; i += this.breakPoint) {
      this.staticKpi.push([...this.staticKpi2].splice(i, this.breakPoint));
    }
    console.log(this.staticKpi);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM