JSFiddle - React, Tailwind, and code Playground

by Tân Nguyễn

JavaScript

let mydata = { 
  section_a: { 
     color: "red", 
     shape: "cube"
  },
  section_b: {
     length: 34
  }
};

let path = "section_b.go.real.deep";
let newKey = "age";
let newVal = 50;

let items = path.split(".");

let item = mydata;

for (var prop of items) {
  if (!item[prop]) {
    item[prop] = {};
  }
  item = item[prop];
}

item[newKey] = newVal;


console.log(mydata);
console.log(mydata.section_b.go.real.deep.age)