UNPKG

@ou-imdt/utils

Version:

Utility library for interactive media development

20 lines (18 loc) 797 B
import splitPropertyPath from './splitPropertyPath.js'; /** * Deletes the property at the specified path of an object. * The path can be specified using dot or bracket notation. * * @param {object} target - The object in which to delete the property. * @param {string} path - The dot/bracket notation path specifying where to delete the property. * @returns {boolean} `true` if the property was successfully deleted, otherwise `false`. */ export default function deleteObjectProperty (target, path) { const parts = splitPropertyPath(path) const success = parts.reduce((current, prop, index, array) => { if (index < array.length - 1) return current?.[prop] if (!Object.hasOwn(current, prop)) return false return delete current[prop] }, target) return success === true }