JSFiddle - React, Tailwind, and code Playground
by cubicalmonkey
JavaScript
function countKeysByKey(json, parentKey = "") {
let count = 0;
for (let key in json) {
const currentKey = parentKey ? `${parentKey}.${key}` : key;
if (typeof json[key] === "object") {
console.log(currentKey)
count += countKeysByKey(json[key], currentKey);
} else {
console.log(`Key '${currentKey}' has ${++count} value(s)`);
}
}
return count;
}
function countObjectsInLastLevel(json, level = 0) {
let count = 0;
for (let key in json) {
if (typeof json[key] === "object") {
count += countObjectsInLastLevel(json[key], level + 1);
} else if (level === 3) {
count++;
}
}
return count;
}
function countKeysByLevel(json, level = 0) {
let count = 0;
level++;
for (let key in json) {
if (typeof json[key] === "object") {
count += countKeysByLevel(json[key], level);
} else {
count++;
}
}
console.log(`Level ${level} keys: ${count}`);
return count;
}
let BY_ICOMS_CABINET = {
"STATE_MD": {
"OLT_mill-e701.dca-anne.md.ftth.rcn.net": {
"": {
"PON_IF_1/2/xp1": {
"CXNK01547B8B": {
"city": null,
"LCP": "",
"apt": null,
"addr": null,
"StatusValue": 108,
"STATE": "MD",
"PON_IF": "PON_IF_1/2/xp1",
"cabinet": null,
"OLT": "mill-e701.dca-anne.md.ftth.rcn.net",
"FSAN": "CXNK01547B8B",
"item": null,
"Updated": "2023-01-12 12:49:29",
"ONTID": ""
},
"CXNK00DF8A1E": {
"STATE": "MD",
"PON_IF": "PON_IF_1/2/xp1",
"cabinet": null,
"LCP": "",
"city": null,
"StatusValue": 108,
"addr": null,
"apt": null,
"FSAN": "CXNK00DF8A1E",
"OLT": "mill-e701.dca-anne.md.ftth.rcn.net",
"ONTID": "",
"Updated": "2023-01-26 17:18:07",
"item": null
},
"CXNK00E06F1C": {
"OLT": "mill-e701.dca-anne.md.ftth.rcn.net",
"FSAN": "CXNK00E06F1C",
"ONTID": "14708508",
"item": null,
"Updated": "2023-01-18...