JSFiddle - React, Tailwind, and code Playground

by Sergei Marochkin

JavaScript

const objectSet = (object, path, value) => {
	result = value
	for (let i = path.length - 1; i > 0; i--) {
		item = {[path[i]]: result}
		result = item
	}
	test[path[0]] = result
}

const test = {
	keepThis: true,
  hello: {
    keepThisToo: true,
  },
};

console.log('before', { keepThis: test.keepThis, hello: test.hello });
objectSet(test, ['hello', 'my', 'dear', 'world'], 12345);
console.log('after', { keepThis: test.keepThis, hello: test.hello });