我有一个对象,其中一个属性是对象数组,如果一个条件为真,则想法是将对象从该数组移动到不新对象。将对象从一个阵列移动到另一个阵列
public $onInit(): void {
this.getTicket();
}
public ticket: any; // Object with the array
public comments: any = []; // New array to move the elements
public getTicket(): void {
this.ticketService
.getTicketComplete(this.$stateParams.ticketID)
.then((response: any) => {
this.ticket = response;
this.stringToDate(this.ticket);
this.ticket.messages.forEach((elem, index) => {
if (elem.type === "comment") {
this.ticket.messages.splice(index, 1);
this.comments.push(elem);
}
});
console.log(this.ticket);
});
}
我已经是下一个问题: 数组有类型的对象,消息和评论,如果数组有2个消息和3条评论,应该推到新阵列3组的意见和离开2条消息,但仅移动2条评论。
任何想法。谢谢你的帮助。
也许原因是你修改'forEach'环内的'array'? – Arg0n
你应该使用'filter()'方法来过滤哪些类型的元素你想要删除** **你已经在你的'comments'对象中插入了你的元素 –
是的想法是循环'数组'寻找元素,并从数组中删除元素,并将其推入一个新的....是否有任何方法可以做到'forEach'循环之外....? –