Say I've got a piece of JSON that I'm storing in a file:
//data.json
{
"table": [
{
"text": "Balance",
"value": "0.00"
}
]
}
Then while I'm iterating over a collection, I want to set a var to the value of the json stored in the file, add some data to it, and then do something else with it. My Java-addled brain tried this:
financials.forEach(function (value) {
...
jsondata = require('data.json')
jsondata.table.push(newTransaction)
...
doSomethingElse(jsondata)
}
What is the best and most idiomatic way to do this in javascript/node? If I try use 'require', I think the caching only sets the value of jsondata once right at first iteration of the loop, and the var jsondata keeps growing instead of resetting, causing data to leak between loops.
(I'm probably also wrong to try this in a foreach.)