Cfscript for loop weirdness

Ok, so I have been developing CF / Lucee apps for a long time. I know there are some things with structs and memory pointers that cause issues. But for the life of me, I can’t figure out why the following is appending the last query result to the array over and over.

REQUEST.response.data = [];
VARIABLES.appendItem = [:];

for ( VARIABLES.item in theQueryName ) {

    VARIABLES.appendItem[ 'id' ] = VARIABLES.item.id;
    VARIABLES.appendItem[ 'title' ] = VARIABLES.item.title;

    REQUEST.response.data.append( VARIABLES.appendItem );

}

writeOutput( serializeJSON( REQUEST.response.data ) );

The above returns THE LAST RECORD IN THE QUERY RESULT over and over:

[{“id”:“1”,“title”:“The item’s title”},{“id”:“1”,“title”:“The item’s title”},{“id”:“1”,“title”:“The item’s title”},{“id”:“1”,“title”:“The item’s title”},{“id”:“1”,“title”:“The item’s title”},{“id”:“1”,“title”:“The item’s title”}]

I know how to fix it, but I just can’t for the life of me figure out why the struct info is not being updated and appended correctly. BTW, The fix is to move the VARIABLES.appendItem = [:] into the loop.

Thanks in advance!

you’re appending a reference to VARIABLES.item, you need to duplicate it

Thanks @Zackster. I knew that there was the ref (I said pointer above), but I figured that with the append it would write out the current data.

Thanks for clearing things up!