@wordpress/core-data
Version:
Access to and manipulation of core WordPress entities.
26 lines (25 loc) • 660 B
JavaScript
// packages/core-data/src/utils/set-nested-value.js
function setNestedValue(object, path, value) {
if (!object || typeof object !== "object") {
return object;
}
const normalizedPath = Array.isArray(path) ? path : path.split(".");
normalizedPath.reduce((acc, key, idx) => {
if (acc[key] === void 0) {
if (Number.isInteger(normalizedPath[idx + 1])) {
acc[key] = [];
} else {
acc[key] = {};
}
}
if (idx === normalizedPath.length - 1) {
acc[key] = value;
}
return acc[key];
}, object);
return object;
}
export {
setNestedValue as default
};
//# sourceMappingURL=set-nested-value.js.map