简体   繁体   English

将对象从一个数组移动到另一个数组

[英]Move object from one array to another

i have one object that one of the properties is an array of objects, the idea is to move objects from that array to no new one if one condition is true. 我有一个对象,其中一个属性是一个对象数组,想法是如果一个条件为真,将对象从该数组移动到没有新对象。

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);
    });
}

the problem that i have is the next one: the array has to types of objects, messages and comments, if the array has 2 messages and 3 comments it should push to the new array 3 comments and leave 2 messages, but is moving only 2 comments. 我遇到的问题是下一个问题:数组必须包含对象类型,消息和注释,如果数组有2条消息和3条注释,它应该推送到新数组3条注释并留下2条消息,但只移动2条评论。

Any idea. 任何的想法。 Thanks for your help. 谢谢你的帮助。

This is the way You do it: 这就是这样做的方式:

 var array1 = [1, 2, 3, 4, 5]; var array2 = []; array1.forEach(function(elem, index) { array1.splice(index, 1); array2.push(elem); }); console.log(array1); //[2, 4] console.log(array2); //[1, 3, 5] 

This is an example of how it can be done: 这是一个如何完成它的例子:

 var array1 = [1, 2, 3, 4, 5]; var array2 = []; for(var i = 0; i < array1.length; i++) { array2.push(array1[i]); array1.splice(i, 1); i--; //decrement i IF we remove an item } console.log(array1); //[] console.log(array2); //[1, 2, 3, 4, 5] 

Specific use-case for you: 特定用例:

let messages = this.ticket.messages;
for(let i = 0; i < messages.length; i++) {
  let message = messages[i];
  if (message.type === "comment") {
    this.comments.push(message);
    messages.splice(i, 1);
    i--;
  }
}

You are removing elements as you loop through your array - this is never a good idea. 您在循环遍历数组时删除元素 - 这绝不是一个好主意。 A better way to solve this issue is to add them to this.comments first and when the foreach is finalized, start looping over this.comments and remove those that are in this array out of the messages. 解决此问题的更好方法是首先将它们添加到this.comments,当foreach完成时,开始循环遍历this.com并从消息中删除此数组中的那些。

Here we go the async way baby: 在这里,我们去异步方式宝贝:

 var array1 = [1, 2, 3, 4, 5]; var array2 = []; async function fillArray() { array1.forEach(function(elem, index) { console.log("elem: ", elem) array2.push(elem); }) } async function emptyArray(){ fillArray().then(() =>{ array1.length = 0; }) } emptyArray().then(() => { console.log("array1: ", array1); //[] console.log("array2: ", array2); //[1, 2, 3, 4, 5] }) 

An another great move, conditional way, cheer: 另一个伟大的举动,有条件的方式,欢呼:

 var array1 = [1, 2, 3, 4, 5]; var array1Length= array1.length; var array2 = []; array1.forEach((elem, index) => { array2.push(elem); if(array2.length === array1Length) array1.length=0 }) console.log("array1: ", array1); //[] console.log("array2: ", array2); //[1, 2, 3, 4, 5] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM