Javascript 用lowdb替换JSON文件中对象中的数组
我一直在努力更新JSON文件。到目前为止,我可以在某些数组中添加新对象。但现在我想删除一个。这是我的代码:Javascript 用lowdb替换JSON文件中对象中的数组,javascript,json,Javascript,Json,我一直在努力更新JSON文件。到目前为止,我可以在某些数组中添加新对象。但现在我想删除一个。这是我的代码: var index = request.body.index; var items = db.get("users") .filter({ "authKey": request.body.authKey }) .map("cart") .map("items") .value(); items.splice(index, 1); var cart = {
var index = request.body.index;
var items = db.get("users")
.filter({ "authKey": request.body.authKey })
.map("cart")
.map("items")
.value();
items.splice(index, 1);
var cart = {
"items": items,
"numbersOfItems": items.length
};
db.get("users")
.assign({ items: items })
.write();
// db.get("users")
// .filter({"authKey": request.body.authKey})
// .set("cart", cart)
// .value();
// db.get("users")
// .filter({"authKey": request.body.authKey})
// .map("cart")
// .assign("items", items)
// .write();`
JSON是:
users : [{
"username": "shop",
"passHash": "something",
"authKey": "something",
"cart": {
"items": [
{
"title": "Product 1",
"price": 1000,
"img": "url",
"description": "Amazing! WOW!"
}
]
}
}]
这里的问题是什么?我想用此代码删除产品1,但它不起作用。您是否检查了您得到的
request.body.index
的值?是的,它是正确的索引。response.body是{authKey:'NWTRssDxY_qrDpZvnB_egwvfmlxzawe+Cgu=kkbvvvm AUqGn%gdmC+=tu',index:0}{authKey:'NWTRssDxY_qrDpZvnB_egwvfmlxzawe+Cgu=kkbvvm AUqGn%gdmC+=tu',仍然没有结果。