Javascript 如何将对象推入 Angular 6 中的对象数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52634288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:00:10 来源:igfitidea点击:
How to push object into an object array in Angular 6?
提问by Oliver
I have an empty object array like this groupList:any = {}and now I want to push an object into it. I am trying to name and description as an object.
我有一个像这样的空对象数组groupList:any = {},现在我想将一个对象推入其中。我试图将名称和描述作为一个对象。
回答by Sajeetharan
It is not an array, array is represented like [] , probably you need
它不是数组,数组表示为 [] ,可能您需要
groupList:any = [];
and then,
进而,
this.groupList.push({name:'you',description:'what is array'});
回答by ankush
app.component.ts
app.component.ts
declare var require: any;
import { Component } from '@angular/core';
import { OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent{
groupList:any = [];
arrayVal:any;
currentVal : any;
title = 'projectchart';
public array = [{"id":1},{"id":3},{"id":5}];
getPrev(){
this.array.forEach((item, index) => {
this.groupList.push(item.id);
});
console.log(this.groupList);
}
}
app.component.html
应用程序组件.html
<button (click) ="getVal()">Previous Value</button>

