Fix this JavaScript function from a rack monitor. Reply with only the complete function snapshot(events, at). No imports, tools, explanations, or test code.
function snapshot(events, at) { const result={top:null,middle:null,bottom:null}; for (const [t,s,v] of events) if(t<=at && v) result[s]=v; return result; }
The input is an array of [integer minute, shelf key, temperature] records, possibly out of chronological order. Return exactly {top,middle,bottom}. For each known shelf, use the latest event time at or before at. If two events for that shelf have the same timestamp, the later array entry wins. An explicit null means that sensor went offline: do not fall back to an older value. Zero and negative temperatures are valid. Ignore unknown shelf names (including inherited object-property names). Never mutate the input. Keep it small.