亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何根據 Typecript 中的某個給定數字對對象數組的元素進行分組

如何根據 Typecript 中的某個給定數字對對象數組的元素進行分組

隔江千里 2023-08-24 17:34:00
我在 Angular 項目中有一個需求。我有一系列對象。它包含 6 個對象。我想將前 3 個分組到一個組中,將其他 3 個分組到另一組中。數組是staticKpi. 這是我的邏輯:  staticKpi=[{},{},{},{},{},{}];  ...  createGrouping() {    var chunks = [],      i = 0,      n = this.staticKpi.length;    while (i < n) {      chunks.push(this.staticKpi.slice(i, (i += 3)));    }    this.staticKpi = chunks;  }最終數組應該是這樣的:staticKpi=[[{},{},{}],[{},{},{}]]但我沒有得到正確的輸出。這是stackblitz。請糾正我的錯誤。
查看完整描述

3 回答

?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

它應該可以幫助你:


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]];

我沒有考慮長度不被3整除的數組。但不難弄清楚如何處理它


查看完整回答
反對 回復 2023-08-24
?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

根據你的 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);

  }

}


查看完整回答
反對 回復 2023-08-24
?
九州編程

TA貢獻1785條經驗 獲得超4個贊

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;

}


查看完整回答
反對 回復 2023-08-24
  • 3 回答
  • 0 關注
  • 235 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號