i am searching a "group function" for an object, which can be created from the following format:
"0_tags.0": {
"common": {
},
"native": {
"topic": "Stromerfassung_251/Zähler0-Leistung"
}
},
"0_tags.0.Various": {
"common": {
},
"native": {
"topic": "Stromerfassung_251/Zähler0-Leistung"
}
},
"0_tags.0.Various.Stromerfassung_251/Zähler0-Leistung": {
"common": {
},
"native": {
"topic": "Stromerfassung_251/Zähler0-Leistung"
}
},
"0_tags.0.Various.Test-String": {
"common": {
},
"native": {
"topic": "Stromerfassung_251/Zähler0-Leistung"
}
},
"0_tags.0.Various.battery_charge": {
"common": {
},
"native": {
"topic": "Stromerfassung_251/Zähler0-Leistung"
}
}
Which should dynamically result in:
"0_tags": {
"0": {
"Various": {
"Stromerfassung_251/Zähler0-Leistung": {
"common": {},
"native": {
"topic": "Stromerfassung_252/Zähler0-Leistung"
}
},
"Test-String": {
"common": {},
"native": {
"topic": "Stromerfassung_253/Zähler0-Leistung"
}
},
"battery_charge": {
"common": {},
"native": {
"topic": "Stromerfassung_254/Zähler0-Leistung"
}
}
}
}
}
My start is as follows:
for (d in jsonArray) {
var item = d.split('.');
if (obj[item[0]] == undefined) {
obj[item[0]] = {};
}
if (obj[item[0]][item[1]] == undefined) {
obj[item[0]][item[1]] = {};
}
obj[item[0]][item[1]][item[2]] = jsonArray[d];
}
But this one is not dynamically doing – instead its "hard-coded" till level 2.
Do you perhaps have any suggestions?
Thank you in advance!
>Solution :
To dynamically create the nested structure without hard-coding the levels, you can use a recursive function. This function will handle the creation of nested objects and update the appropriate properties at each level.
The createNestedObject function takes the parent object obj, the array of keys, and the value to be inserted at the deepest level. It recursively creates nested objects until the last key is reached, where it assigns the value.
Using this approach, you can handle any level of nesting in the input data dynamically, resulting in the desired output format. The result object will contain the transformed data as specified.
Here’s a possible implementation in JavaScript:
function createNestedObject(obj, keys, value) {
let key = keys.shift();
if (keys.length === 0) {
obj[key] = value;
} else {
if (obj[key] === undefined) {
obj[key] = {};
}
createNestedObject(obj[key], keys, value);
}
}
const result = {};
for (const key in tagObject) {
const value = tagObject[key];
const keys = key.split('.');
createNestedObject(result, keys, value);
}
console.log(JSON.stringify(result, null, 2));
<script>
const tagObject = {
"0_tags.0": {
"common": {},
"native": {
"topic": "Stromerfassung_251/Zähler0-Leistung"
}
},
"0_tags.0.Various": {
"common": {},
"native": {
"topic": "Stromerfassung_251/Zähler0-Leistung"
}
},
"0_tags.0.Various.Stromerfassung_251/Zähler0-Leistung": {
"common": {},
"native": {
"topic": "Stromerfassung_251/Zähler0-Leistung"
}
},
"0_tags.0.Various.Test-String": {
"common": {},
"native": {
"topic": "Stromerfassung_251/Zähler0-Leistung"
}
},
"0_tags.0.Various.battery_charge": {
"common": {},
"native": {
"topic": "Stromerfassung_251/Zähler0-Leistung"
}
}
};
</script>