JSFiddle - React, Tailwind, and code Playground

by Alexander

JavaScript

console.clear();

const setf = (obj, path, val = null) => {
	path = path.split('.');
	while (1 < path.length) {
		const [head, ...tail] = path;
		path = tail;
		if (void 0 === obj[head]) obj[head] = {};
		obj = obj[head];
	}
	obj[path[0]] = val;
};


var obj
//obj = {}
//obj = { a1: 'abc' };
obj = { a: { b: 456 }, a1: 'abc' };

setf(obj, 'a.b', 123);

console.log(JSON.stringify(obj));
console.log(obj);