Sunday 4 June 2017

Javascript Object Not Mutating When Passed As Param For JS Function In NodeJS

Having an issue here with this function, it's meant to loop through an array and check if the items within that array are keys in the object supplied to the function and on the last iteration return the supplied object with its updates. It is meant to delete any item that's in the guarded array.

The expected behaviour is for this function to return the object omitting the guarded items, but I get the object returned untouched.

I have tried logging to the console within the if statement to see if each item was found within the object and they were.

Also I am calling this within a promise and resolve with the value, and I am writing this in a Nodejs project.

Please help, thanks!

/**
 * Hide all items in guarded array
 * @param item Object
 * @return Object
*/
self._hide = function(item)
{
    if( self.guarded.length < 1 ) return item;

    for(var i = 0; i < self.guarded.length; i++)
    {   
        if(item[self.guarded[i]])
        {
            delete item[self.guarded[i]];
        };

        if(i >= (self.guarded.length - 1))
        {
            return item;
        }
    }
}



via Daniel Barde

No comments:

Post a Comment