function set(object, path, value) {
const isObjectInvalid = !object || typeof object !== 'object';
if (isObjectInvalid || !path) {
return null;
}
if (typeof path === 'string') {
path = path.replaceAll('[', '.');
path = path.replaceAll(']', '.');
path = path.split('.');
}
path = path.filter(Boolean);
let i;
let currentKey;
let currentItem = object;
for (i = 0; i < path.length; i++) {
currentKey = path[i];
if (!Object.prototype.hasOwnProperty.call(currentItem, currentKey)) {
const isNonArrayMissingIndex = isNaN(path[i + 1] && Number(path[i + 1]));
if (isNonArrayMissingIndex) {
currentItem[currentKey] = {};
} else {
currentItem[currentKey] = [];
}
}
if (i === path.length - 1) {
currentItem[currentKey] = value;
} else {
currentItem = currentItem[currentKey];
}
}
return object;
}
Read-only