Skip to content Skip to sidebar Skip to footer

Dynamically Change Nested Json

I'm trying to put a value in a JSON with this code: result = {} result[idHour] = {}; result[idHour][date.minute()] = req.body.generated; idHour is a variable I have defined ea

Solution 1:

Is it possible you meant date.getMinutes()? This seems to do the trick for me...

<script>var idHour = "18";
var generated = 1000;
var date = newDate();
result = {};
result[idHour] = {};
result[idHour][date.getMinutes()] = generated;
document.write(JSON.stringify(result, null, 2));
</script>

The result was: { "18": { "35": 1000 } }

Also date.getMinutes() doesn't return a string, so your JSON stringifier may want you to convert it with toString().

Post a Comment for "Dynamically Change Nested Json"