如何获取此JSON中的元素数量?
gettingTheLocalStorageData = {
"sortable2": {
"content": "example_panel_2",
"columns": "3",
"panel-size": "large",
"childs": {
"item-1-sortable2": {
"header": "Feeds 1 ",
"body": ""
},
"item-2-sortable2": {
"header": "Feeds 2",
"body": ""
},
"item-3-sortable2": {
"header": "Feeds 3",
"body": ""
},
"item-4-sortable2": {
"header": "Feeds 4",
"body": ""
},
"item-5-sortable2": {
"header": "Feeds 5",
"body": ""
}
}
},
"sortable3": {
"content": "example_panel_3",
"columns": "3",
"panel-size": "large",
"childs": {
"item-0-sortable3": {
"header": "Feeds 1 ",
"body": ""
},
"item-1-sortable3": {
"header": "Feeds 2",
"body": ""
},
"item-2-sortable3": {
"header": "Feeds 3",
"body": ""
},
"item-3-sortable3": {
"header": "Feeds 4",
"body": ""
},
"item-4-sortable3": {
"header": "Feeds 5",
"body": ""
}
}
}
}
这应该返回2个项目(sortable2
,sortable3
),但它返回的是659
。我尝试过的几个步骤是:
console.log(gettingTheLocalStorageData .length); // = 659
和
console.log(Object.keys(gettingTheLocalStorageData).length); // = 659
任何可能的解决方案?
答案 0 :(得分:0)
您可以使用Object.keys():
Object.keys(getLocalStoragedata).length
答案 1 :(得分:0)
我只需要解析它就可以了。
已添加
gettingTheLocalStorageData = JSON.parse(gettingTheLocalStorageData);
现在它返回正确的数字
与@Deda一样
答案 2 :(得分:-1)
首先解析JSON
let parsed = JSON.parse(getLocalStoragedata)
console.log(Object.keys(parsed).length); // 2