对于简单的localstorage,我们使用localstorage.length。但我正在使用一个插件localStorageDB,它具有如下所示的JSON输出。我想计算“购物车”商品('数据' - >'购物车')。
每当我添加Products时,localstorage.length的输出为1。但是,产品会添加到“数据” - >“购物车”中。
{
"tables": {
"cart": {
"fields": [
"ID",
"item",
"quantity",
"cost"
],
"auto_increment": 3
}
},
"data": {
"cart": {
"1": {
"ID": 1,
"item": "Nature Fresh Atta Chakki Fresh 10 kg",
"quantity": "1",
"cost": "252"
},
"2": {
"ID": 2,
"item": "Fortune Refined Oil Soya Bean 1 Lt",
"quantity": "1",
"cost": "85"
}
}
}}
更多信息: 我使用'cart.serialize()'插件语法获得了序列化输出 Screenshot
答案 0 :(得分:1)
syntax: Object.keys(obj).length);
var data = {
"tables": {
"cart": {
"fields": [
"ID",
"item",
"quantity",
"cost"
],
"auto_increment": 3
}
},
"data": {
"cart": {
"1": {
"ID": 1,
"item": "Nature Fresh Atta Chakki Fresh 10 kg",
"quantity": "1",
"cost": "252"
},
"2": {
"ID": 2,
"item": "Fortune Refined Oil Soya Bean 1 Lt",
"quantity": "1",
"cost": "85"
}
}
}
}
console.log(Object.keys(data['data']['cart']).length);
答案 1 :(得分:0)
localStorageDB
插件具有内置方法rowCount()
,用于计算表格中的条目。
所以,这样的事情应该有效:yourLib.rowCount('cart')