If you go through the whole blockchain and record every transaction as you plan, you'll probably run into some sort of memory issue. You do have the right approach, though something like this would save you some memory (using JS in a psuedocode kinda way :p):
var utxos = {}; // Make an object/list
getblock {
foreach block.tx {
getrawtransaction {
foreach tx.vin {
if (utxos[vin.txid+':'+vin.vout])
unset utxos[vin.txid+':'+vin.vout];
}
foreach tx.vout {
utxos[tx.txid+':'+vout.n] = vout; // Note that the vout object does NOT contain the TX ID
}
}
}
}
So basically, you are only storing the transactions that are unspent. Note this approach may be inaccurate due to doublespends and the like.