JSFiddle - React, Tailwind, and code Playground

by Joshua Koudys

JavaScript

var state = {foo: {bar: {baz: 123}}, otherthing: 'phoey'};
var keys = ['foo', 'bar', 'baz']
var updateVal = 789;

for (var i = keys.length - 1; i > 0; i--) {
    var val = updateVal;
    updateVal = {};
    updateVal[keys[i]] = val;
}

// Note: use setState() in React instead, which will merge the properties
Object.defineProperty(state, keys[0], {value: updateVal, writeable: true});

document.body.textContent = (JSON.stringify(state));