4972

Deleting JS Object Properties a few levels deep

Question:

I have an object like this:

var myObj = { first: { sub: { level: "some text", level2: "some more text" }, sub2: { level3: "Something" } }, second: { stuff: "More stuff...lots of stuff" } }

what I want to be able to do is say

delete myObj.first.sub.level

But I won't know what is being passed, or how many levels deep I need to go in order to remove the correct property, meaning it could simply be:

Storage.removeItem('myObj.first'); // This is currently working

Or something more complex:

Storage.removeItem('myObj.first.sub2.level3'); // This doesn't work because I'm more than 1 level into the object.

I'm kind of stuck, because I can get to the point where I have the key "level3" and it's property "Something", but I can't figure out how to back-step correctly in order to delete the full section of that object.

I need to replicate it's place in myObj so I can delete the full passed object.

'myObj.first.sub.level3'

If that makes sense...

Answer1:

It's not pretty, but you could use something like this:

function deepDelete(target, context) { // Assume global scope if none provided. context = context || window; var targets = target.split('.'); if (targets.length > 1) deepDelete(targets.slice(1).join('.'), context[targets[0]]); else delete context[target]; } deepDelete('first.sub.level3', myObj); deepDelete('myObj.first.sub2.level3');

It would probably be a good idea to modify it to test for typeof context[targets[0]] !== 'undefined' before descending. How exactly you react to that (return false, throw, or whatever else) would depend on how you're using it.



Recommend

  • PowerShell Remove-Item not waiting
  • Auto turn off toggle button when other one is turned on
  • Disable or hide options in combo box VBA Excel
  • Swift 3 : How to export video with text using AVVideoComposition
  • How to get or calculate size of Azure File/Share or Service
  • Where to save the local DB created for iphone app?
  • get passwords from chrome
  • Does the Azure table storage API cache results?
  • is there a way to update filter with async data
  • Web.config system.webserver errors
  • How to assign byte[] as a pointer in C#
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • How solve “Qt: Untested Windows version 10.0 detected!”
  • Visual Studio 2010 debugger build correctly - compiler pdb and linker pdb not in synch?
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Database structure design with variable amounts of fields
  • Highlight one bar in a series in highcharts?
  • Why value captured by reference in lambda is broken? [duplicate]
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Android screen density dpi vs ppi
  • Regex thinks I'm nesting, but I'm not
  • What is the “return” in scheme?
  • How do I fake an specific browser client when using Java's Net library?
  • How to get a value (ex: baseURL) in every Karate feature?
  • How would I use PHP exceptions to define a redirect?
  • How to extract text from Word files using C#?
  • Javascript Callbacks with Object constructor
  • How can I use Kendo UI with Razor?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Apache 2.4 - remove | delete | uninstall
  • 0x202A in filename: Why?
  • How to disable jQuery.jplayer autoplay?
  • Why joiner is not used after Sequence generator or Update statergy
  • need help with bizarre java.net.HttpURLConnection behavior
  • File not found error Google Drive API
  • Checking variable from a different class in C#
  • Recursive/Hierarchical Query Using Postgres
  • UserPrincipal.Current returns apppool on IIS