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

4

我有一个对象,其中一个属性是一个对象数组,想法是如果满足条件,将对象从该数组移动到新的数组中。

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个评论。
有什么想法吗?谢谢您的帮助。

3
可能的原因是你在forEach循环内修改了array数组? - Arg0n
1
你应该使用filter()方法来过滤你想要在comments对象中删除的元素类型,这应该是在你已经插入元素之后进行的。 - Alex Beugnet
是的,思路是循环搜索array中的元素并将其从数组中删除,然后将其推入新的数组...除了forEach循环之外,还有其他方法可以实现吗? - Miguel Frias
3个回答

14

这是做法:You

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]

这是一个示例,说明如何完成:

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]

您的特定使用情况:

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

即使我先将元素推入数组,然后再将其删除,仍会留下一条评论,其余评论都被推入新数组中。 - Miguel Frias
1
请使用更新后的答案,将您的forEach循环替换为for循环。您的问题不在于推入元素,而是数组现在包含了一个元素少。而且我不知道任何一种方法可以在forEach循环中递减index - Arg0n
谢谢你的回答,那就是问题所在,很高兴知道我不能使用 forEach 来递减索引,感谢你的帮助,也感谢其他提供想法的人们。 - Miguel Frias
为什么原始解决方案无法运行?乍一看似乎很有效,如果在这里添加一些注释会更好。 - Webwoman
@Webwoman 在第一个代码片段中,index 没有被递减,因此我们只移动了一半的元素(因为在循环时数组已经被改变)。 - Arg0n

0

我们来试试异步编程吧:

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

又一个伟大的举动,条件方式,加油:

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] 


0
你正在遍历数组时删除元素 - 这绝不是一个好主意。解决这个问题的更好方法是首先将它们添加到 this.comments 中,当 foreach 完成后,开始遍历 this.comments 并将那些在此数组中的消息删除。

从最后一个索引循环到第一个索引 - JanBrus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接