@agile-ts/multieditor
Version:
Simple Form Manager for UI-Frameworks
21 lines (18 loc) • 596 B
JavaScript
import { copy } from '@agile-ts/utils';
function updateNestedProperty(obj, path, value) {
const updatedObject = copy(obj);
let schema = updatedObject;
const pathParts = Array.isArray(path) ? path : path.split(".");
const pathPartsLength = pathParts.length;
if (pathPartsLength <= 0)
return obj;
for (let i = 0; i < pathPartsLength - 1; i++) {
const pathPart = pathParts[i];
if (!schema[pathPart])
schema[pathPart] = {};
schema = schema[pathPart];
}
schema[pathParts[pathPartsLength - 1]] = value;
return updatedObject;
}
export { updateNestedProperty };