Remove own nested properties (props) from a object using prop-path notation
npm install remove-prop
obj
: takes a object to perform prop deletionpath
: takes a string specifying the prop to be deletedparserOptions
: takes of object of options
throws: PropMissing
and ObjectInvalid
errors
returns true
if succeeds otherwise false
separator
(default:.
): specify the prop separator which is to be used during parsing of the pathoptionalChar
(default:?
): specify the optional character
const { removeOwnProp } = require("remove-prop");
const universe = {
galaxies: {
milkyway: {
sun: "Burning",
earth: "Life",
},
andromeda: {
life: "not found"
},
}
};
- Path =
"galaxies"
// the prop must exist on the object otherwisePropMissing
error will be thrown - Path =
"?galaxies"
// the prop is optional and only gets deleted if it exists
"galaxies"
: delete universe["galaxies"]"galaxies.milkyway"
: delete universe.galaxies["milkyway"]"galaxies.milkyway.?sun"
: delete universe.galaxies.milkyway["sun"]
removeOwnProp(universe, "galaxies"); // => true
console.log(universe); // {}
removeOwnProp(universe, "galaxies.milkyway.sun"); // => true
removeOwnProp(universe, "galaxies.?beautifulGalaxy"); // => false
removeOwnProp(universe, "galaxies.milkyway.?blackHole"); // => false
removeOwnProp(universe, "galaxies.milkyway.?earth"); // => true
- Cyclic Objects
- Symbolic keys