JSFiddle - React, Tailwind, and code Playground

by TheDiamondDoge

JavaScript

let obj = {
	general: {
  	projectDescription: true
  },
  milestones: [
  	{
    label: "Required!", 
    arr: [
    	{label: "hi!"}
    ]
    }
  ]
};


let path = "milestones[0].arr[0].label";
/* let path = "general.projectDescription"; */

console.log("FUNC", getPropValue(obj, path));


console.log(obj.milestones[0].label);
console.log(obj["milestones[0].label"]);


function getPropValue(obj, path) {
	let edittedPath = path.replace(/]/g, "");
  let arr = edittedPath.split(/[\.\[]/);
  let tmpVal = obj;
  for (let i = 0; i < arr.length; i++) {
  	tmpVal = tmpVal[arr[i]];
  }
  return tmpVal;
}