typescript 如何使用打字稿将对象推入数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46249472/
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-09-09 07:47:31  来源:igfitidea点击:

How to push an object into an array with typescript

typescript

提问by Tri Nguyen

i have an array name dish and have a form. After the form submited, data push to dish. I have tried to use push method to add that into an array, but it's have error. How i can do that with typescript ? Thanks you very much. Class object.

我有一个数组名称盘并有一个表格。表单提交后,数据推送到盘。我尝试使用 push 方法将其添加到数组中,但有错误。我怎么能用打字稿做到这一点?非常感谢你。类对象。

export interface Dish {
   id: number;
   name: string;
   image: string;
   category: string;
   label: string;
   price: string;
   featured: boolean;
   description: string;
   comments: Comment[];
}

I have created an object name commentData from class comment to receive all data from form after submit. I also created an object name dish from class Dish. How to push object commentData to object dish.comments

我从类 comment 创建了一个对象名称 commentData 以在提交后从表单接收所有数据。我还从 Dish 类创建了一个对象名称菜肴。如何将对象commentData推送到对象dish.comments

export interface Comment {
   rating: number;
   comment: string;
   author: string;
   date: string;
}

My git : https://github.com/zymethyang/Ionic_HKUST

我的 git : https://github.com/zymethyang/Ionic_HKUST

回答by Dr. X

let myArray= [];
let commentData =  {} as Dish;
commentData.id = 3;
commnetData.name = 'something';
...

myArray.push(commentData);

It will work...

它会工作...

回答by amal

If you simply need to add the new commentDataafter each form submission (if I understood you correctly), all you need is this every time you want the new comments to be pushed to the existing dish.comments,

如果您只需要commentData在每次提交表单后添加新的(如果我理解正确的话),那么每次您希望将新评论推送到现有评论时,您只需要这样做dish.comments

this.dish.comments = this.dish.comments.push(this.commentData); // assuming these are class properties, hence the 'this' usage

Does that work for you?

那对你有用吗?

EDIT

编辑

Modify the first line

修改第一行

this.commentData = this.comment.value;

in dismiss()method to this,

dismiss()这个方法中,

this.commentData.author = this.comment.get('author').value;
this.commentData.rating = this.comment.get('rating').value;
this.commentData.comment = this.comment.get('comment').value;

回答by FiringBlanks

If you need to add multiple objects to an array inside a loop:

如果需要将多个对象添加到循环内的数组中:

let thingsArray = [];

someArray.forEach(doc => {
    let thingsObj = {} as Thingy;

    thingsObj.weekDue = doc.name;
    thingsObj.title = doc.age;
    thingsArray.push(thingsObj);

}).then(() => {
    console.log(thingsArray);
}

回答by Danushka Amith Weerasingha

let arraylist= [];
let obj1 = {} as Class;
obj1.id = 1;
obj1.name = 'text';
let obj2 = {} as Class;
obj2.id = 1;
obj2.name = 'text';
arraylist =[obj1,obj2 ];

let arraylist= [];
let obj1 = {} as Class;
obj1.id = 1;
obj1.name = 'text';
let obj2 = {} as Class;
obj2.id = 1;
obj2.name = 'text';
arraylist =[obj1,obj2 ];

This work for me but I tried it with Classes

这对我有用,但我用 Classes 试过了