Remove function in array (possible bug?)

I am trying to create a function that removes an element from an array. Here is the code for the function:

// Removes an element from an array.
    // String value: the value to search and remove.
    // return: an array with the removed element; false otherwise.
    Array.prototype.remove = function(value) 
    {
        var idx = _.indexOf(this,value);
        if (idx != -1) 
        {
            return this.splice(idx, 1); // The second parameter is the number of elements to remove.
        }
        return false;
    };
    
    
    // This code runs once per frame. Build units and command peasants!
    // Destroy the ogre base within 180 seconds.
    // Run over 4000 statements per call and chooseAction will run less often.
    // Check out the green Guide button at the top for more info.
    
    var base = this;
    
    /////// 1. Command peasants to grab coins and gems. ///////
    // You can only command peasants, not fighting units.
    // You win by gathering gold more efficiently to make a larger army.
    // Click on a unit to see its API.
    var items = base.getItems();
    var peasants = base.getByType('peasant');
    
    for (var peasantIndex = 0; peasantIndex < peasants.length; peasantIndex++) 
    {
        var peasant = peasants[peasantIndex];
        var item = peasant.getNearest(items);
        if (item)
        {
            var removed_item = items.remove(item); 
            //The item is correctly removed (i believe...). If someone checks the content of items it is not there.

            base.command(peasant, 'move', item.pos);
            var items_new = items;
            var item2 = peasant.getNearest(items_new);
            //But when i use the getNearest it returns the removed element.
        }
    
    }

My problem is that when i use the function it seems to work correct and the element seems to be removed but when i call the getNearest function it returns the removed element. Is it my fault or something else??? Thanks in advance :smile:

I guess this is related with the problem I’m currently having and I think I know the origin of it.

I have the following test code base in Gold Rush:

function test(array) {
    array.push(321);
}

var array = [123];
test(array);
this.say(array);

What would be expected is that Tharin says “123, 321”, but he just says “123”. My guess is that arrays are copied when used as a function argument and that’s why the changes don’t reflect outside of the function.

Sadly I don’t even know how to temporarily fix this, so I’m stuck until this gets resolved.

If you add the array as a property of this and make your function as a method of this then you can modify the list inside the function.

this.test = function(){
   this.array.push(321);
}

this.array = [123];
this.test();
this.say(this.array());

Sorry about this; the API protection is being overzealous again. Tracking the bug over here. I won’t be able to fix it very quickly, so workarounds like @dwhittaker’s are a good idea for now.

I am not sure that i have the same problem. In my case the function works just fine and it removes the element. If someone checks the lenght of the array before the function and after he will see that it has changed. The problem is that the getNearest() function seems to cache somehow the array. If i use this code:

 if (item)
    {
        base.command(peasant, 'move', item.pos);
        var removed_item = items.remove(item);
        
        items = items.slice(0);
        var item2 = peasant.getNearest(items);
        var removed_item2 = items.remove(item2);
}

it works as exepected and finds the next closest item. But in theory i shouldn`t have to copy the elements of the array .