第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何根據(jù)角度8中的條件從對(duì)象數(shù)組中獲取最后一個(gè)對(duì)象

如何根據(jù)角度8中的條件從對(duì)象數(shù)組中獲取最后一個(gè)對(duì)象

智慧大石 2023-10-04 14:22:33
我有一些對(duì)象,我使用settimeout函數(shù)接收一個(gè)又一個(gè)對(duì)象,然后每次將其推入數(shù)組以填充到表中。這是在我的項(xiàng)目中動(dòng)態(tài)出現(xiàn)的,但僅供參考,我settimeout在這里使用和硬編碼?,F(xiàn)在我的問題是,每當(dāng)我使用 接收數(shù)據(jù)時(shí)settimeout,我需要通過使用vehicle_number 進(jìn)行過濾來獲取最后一個(gè)對(duì)象(如果它包含相同的vehicle_number,我需要獲取該vehicle_number 的最后一個(gè)對(duì)象),并且需要再次填充/推入表中。這是我嘗試過的代碼。home.component.html<div><table><tr *ngFor="let x of groupList"><td>{{x.vehicle_number}}</td><td>{{x.status}}</td></tr></table></div>home.component.tsimport { Component, OnInit } from '@angular/core';@Component({  selector: 'app-home',  templateUrl: './home.component.html',  styleUrls: ['./home.component.css']})export class HomeComponent implements OnInit {imageSource :any;statusdata1: any;vehicle_number:any;groupList:any = [];constructor() {}  ngOnInit() {     /* First data */    this.statusdata1 = {"vehicle_number":1,"status":"red"};     this.groupList.push(this.statusdata1);     console.log(this.groupList);     /* second data */    setTimeout (() => {        this.statusdata1 = {"vehicle_number":1,"status":"green"};         this.groupList.push(this.statusdata1);         console.log(this.groupList);      }, 5000);   /* Third data */      setTimeout (() => {        this.statusdata1 = {"vehicle_number":2,"status":"yellow"};         this.groupList.push(this.statusdata1);         console.log(this.groupList);      }, 10000);  }}
查看完整描述

1 回答

?
白衣非少年

TA貢獻(xiàn)1155條經(jīng)驗(yàn) 獲得超0個(gè)贊

您可以編寫一個(gè)快速函數(shù)來更新值,如下所示


private updateGroupList(statusData: any) {

? for (const key in this.groupList) {

? ? if (this.groupList[key].vehicle_number === statusData.vehicle_number) {

? ? ? this.groupList[key].status = statusData.status;

? ? ? return;

? ? }

? }

? this.groupList.push(statusData);

}

您可以將this.groupList.push()中的所有ngOnInit()內(nèi)容替換為this.updateGroupList(this.statusdata1).


ngOnInit() {

? /* First data */

? this.statusdata1 = {"vehicle_number":1,"status":"red"};

? this.updateGroupList(this.statusdata1);

? console.log(this.groupList);


? /* Second data */

? setTimeout (() => {

? ? this.statusdata1 = {"vehicle_number":1,"status":"green"};

? ? this.updateGroupList(this.statusdata1);

? ? console.log(this.groupList);

? }, 5000);


? /* Third data */

? setTimeout (() => {

? ? this.statusdata1 = {"vehicle_number":2,"status":"yellow"};

? ? this.updateGroupList(this.statusdata1);

? ? console.log(this.groupList);

? }, 10000);

}

查看完整回答
反對(duì) 回復(fù) 2023-10-04
  • 1 回答
  • 0 關(guān)注
  • 121 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)