简体   繁体   English

Javascript Array.splice 在我的 Angular 项目中停止正常工作

[英]Javascript Array.splice stopped working correctly in my Angular project

I was working on a project in Angular 4 and needed to batch through an array of ids.我正在使用 Angular 4 开发一个项目,需要通过一组 id 进行批处理。 I would send the removed items to an Observable function, and when the subscription returned I would check the remaining array before looping the batch.我会将删除的项目发送到 Observable 函数,当订阅返回时,我会在循环批处理之前检查剩余的数组。

The first time I did this it worked perfectly, the original array was reduced over time to zero items.我第一次这样做时效果很好,原始数组随着时间的推移减少到零项。 The second time I did this it no longer worked.我第二次这样做它不再起作用。 Every time the subscription returns the original array was suddenly full again.每次订阅返回时,原始数组突然又满了。

The original array was an element of the component that was making the subscription.原始数组是进行订阅的组件的一个元素。

RemoveItemsArray : Array<number>;

I had an event handler function initializing the values and calling the batch function.我有一个事件处理函数初始化值并调用批处理函数。

HandleActions( action, payload )
{
    switch( action )
    {
        case “finish”:
            DoSomething();
            break;
        case “remove”:
            this.RemoveItemsArray = payload.items;
            this.ProcessRemoveItems();
            break;
    }
}

The Batching function would console log the array being correctly sized before the API function was called. Batching 函数将在调用 API 函数之前控制台记录数组的大小正确。 Then in the response suddenly all the items were back and therefore it became an infinite loop.然后在响应中突然所有的项目都回来了,因此它变成了一个无限循环。

ProcessRemoveItems()
{
    let executeItems = this.RemoveItemsArray.splice(0, 10);
    this.service.RemoveItemsAPI( executeItems )
        .subscribe ( response =>  {
            if( response.StatusCode == 200 )
            {
                if( this.RemoveItemsArray.length > 0 )
                {
                    this.ProcessRemoveItems();
                }
                else
                {
                    this.HandleAction(“finish”,null);
                }
            }
        });
}

Most likely you're modifying the array elsewhere in the middle of draining it.您很可能在排空阵列的过程中在其他地方修改阵列。

You'll want to make a copy of the array if this is the case.如果是这种情况,您将需要制作数组的副本。 I can't tell from the given code what modifications are being made, so all I can do is give you a couple guesses.我无法从给定的代码中看出正在进行哪些修改,所以我所能做的就是给你几个猜测。

Solution 1: Copy payload.items to RemoveItemsArray .解决方案 1:将payload.items复制到RemoveItemsArray

This assumes RemoveItemsArray is not modifed during draining这假设RemoveItemsArray在排空期间未修改

HandleActions( action, payload )
{
    switch( action )
    {
        case “finish”:
            DoSomething();
            break;
        case “remove”:
            this.RemoveItemsArray = [...payload.items];
            this.ProcessRemoveItems();
            break;
    }
}

Solution 2: Pass an all new array to ProcessRemoveItems解决方案 2:将一个全新的数组传递给ProcessRemoveItems

HandleActions( action, payload )
{
    switch( action )
    {
        case “finish”:
            DoSomething();
            break;
        case “remove”:
            this.RemoveItemsArray = payload.items;
            this.ProcessRemoveItems([...payload.items]);
            break;
    }
}

ProcessRemoveItems(items)
{
    let executeItems = items.splice(0, 10);
    this.service.RemoveItemsAPI( executeItems )
        .subscribe ( response =>  {
            if( response.StatusCode == 200 )
            {
                if( items.length > 0 )
                {
                    this.ProcessRemoveItems(items);
                }
                else
                {
                    this.HandleAction(“finish”,null);
                }
            }
        });
}

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

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