0
lowdb
の構文で入れ子になったJSON配列/オブジェクトにアクセスする方法を知りたいですか?'lowdb'で入れ子になったJSONオブジェクト/配列にアクセスするには?
基本的に私はlowdb
ファイルとしてこのような構造を持っている:
{
"57": {
"hour": [],
"day": [],
"week": [],
"month": []
},
"58": {
"hour": [],
"day": [],
"week": [],
"month": []
}
}
今私は動的に配列を埋めるためにしたいが、私はに任意の値をプッシュすることはできませんよ...
私のコード:
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync')
const adapter = new FileSync('myFile.json')
const db = low(adapter);
....
// add new object
db.set("57", { hour: [], day: [], week: [], month: [] }).write();
console.log(db.get('57').value().hour[0]); // prints ofc 'undefined'
console.log(db.get('57').value().hour); // prints [] which is correct
// both commands are not working | for test just push a single item
// later the item will be another object
// db.get('57').value().hour.push('item').write();
// db.get('57').hour.push('item').write();
// if copying first it's working well
var tmp = db.get('57').value().hour;
tmp.push('item');
console.log(tmp); // outputs [ 'item' ]
入れ子構造のため、lowdbではこれができないと感じました。可能であれば、誰かが私にどのように教えてくれますか?