【发布时间】:2018-04-05 10:49:34
【问题描述】:
我想知道如何使用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 无法做到这一点。如果可能的话,谁能告诉我怎么做?
【问题讨论】:
标签: javascript arrays json