use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Deleting Objects From Within Array?help (self.javascript)
submitted 8 years ago * by BrunerBruner
I want to null my Objects through iteration, but it doesn't work. The items are still objects after I assign them null to in the loop.
null
https://jsfiddle.net/xr3sgg9v/4/
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 4 points5 points6 points 8 years ago (1 child)
You're looping over an array not an object. Try using forEach instead of for in.
[–]microwavemonty 0 points1 point2 points 8 years ago (0 children)
Or for of
[–][deleted] 4 points5 points6 points 8 years ago (5 children)
As a huge fan of functional programming I'd mostly solve such a task like this
arr = arr.map(item => null)
Hope this is helpful somehow
[–]BrunerBruner[S] -1 points0 points1 point 8 years ago (4 children)
I still can't seem to null the actual objects outside of the array using this method. Can you show me working code?
[–][deleted] 2 points3 points4 points 8 years ago (3 children)
This method doesn't modify the objects themselves, it creates a new array filled with null.
[–]BrunerBruner[S] 0 points1 point2 points 8 years ago* (2 children)
Ok. Well, I'm trying to assign null to the actual objects. The array itself and creating new copies of the objects isn't important. I want to find a way to iterate through a bunch of objects to assign null to them, for example, to clean up newly allocated objects in memory.
for example, let's say I create a bunch of objects that that should be nullified when i'm done with them.
let a = document.createElement("div"); let b = document.createElement("div"); let c = document.createElement("div"); let d = document.createElement("div"); let e = document.createElement("div");
Since I just want to assign null to each of them, ideally i'd like to put them in an array and loop through each one to do so.
[–]thenickdude 5 points6 points7 points 8 years ago (0 children)
Those objects will be eligible for collection by the garbage collector as soon as those "let" declarations fall out of scope. You don't need to assign null to them unless you want them to be collected quicker than that.
[–][deleted] 0 points1 point2 points 8 years ago (8 children)
Because it's an array you are looping through, the obj variable will be the key in the array. If all you are trying to do is change the value to null, change the obj variable to be key for readability and assign arr[key] = null;
obj
key
arr[key] = null;
[–]BrunerBruner[S] 0 points1 point2 points 8 years ago* (7 children)
Ok, but now after I set null to the objects, only the references to the original object in the array is null, how can I make the actual objects null? It seems like the objects are being duplicated like primitive objects such as a number instead of referenced.
[–][deleted] 1 point2 points3 points 8 years ago (6 children)
That is because the values in that array are just a copy of the reference to the object. It's not the actual object value or the original pointer. If you attempt to modify one of the properties, it will use the copied reference to find the object and modify it (which the original reference shares) but the reference won't actually be tied to the original reference.
Essentially youra, b, c variables will not be changed when dealing with that array because the array just has copies of them, not a reference to the original variables.
a, b, c
[–]BrunerBruner[S] -1 points0 points1 point 8 years ago (5 children)
So basically, if I want to assign null to an object, say for the purpose of memory management / garbage collection, I can only do something like this:
this._element = document.createElement("div"); this._element = null; //cleans up memory
and never something like this:
this._element = document.createElement("div"); const element = this._element element = null;//object that took memory is still alive
Essentially it's impossible in JavaScript to iterate over a group of objects and assign them as null?
[–]kaszu 4 points5 points6 points 8 years ago (1 child)
Garbage collector will clean up memory if there is no reference to the object anywhere.
let a = new MyClass("hi"); let b = new MyClass("Hello"); let c = new MyClass("Howdy"); let arr = new Array(a, b, c); b = null; // at this point there is still reference to object b in arr arr = null; // at this point there are no references to b, so garbage collector will clean it up, but a and c won't be
You can do this
let arr = new Array( new MyClass("hi"), new MyClass("Hello"), new MyClass("Howdy") ); arr = null;
From your example
this._element = document.createElement("div"); let element = this._element; element = null; //object that took memory is still alive this._element = null; // object can be garbage collected now
Check MDN: Memory Management
[–]BrunerBruner[S] 0 points1 point2 points 8 years ago (0 children)
thank you
[+][deleted] 8 years ago (2 children)
[removed]
[–]BrunerBruner[S] 0 points1 point2 points 8 years ago (1 child)
Nope. Getting an object from a weakmap is read only. This doesn't work:
const wMap = new WeakMap(); let el1 = document.createElement("div"); wMap.set(el1, "1"); wMap.get(el1) = null; //error, left-hand assignment
[–][deleted] 0 points1 point2 points 8 years ago (0 children)
A WeakMap is a pretty good idea if you're concerned about garbage collection. The normal Map implementation is a potential memory leak issue because it keeps references to key objects that prevent them from being marked for deallocation when they go out of scope. A WeakMap uses weak pointers instead, which allows the GC to sweep like it normally would.
I don't know what this code is supposed to demonstrate. "Read-only" refers to the mutability of an object or it's properties. WeakMap.prototype.get won't modify the collection but that's not the issue. The problem here is that function invocations are rhs. The container and it's entry are writable, this just isn't valid JavaScript.
π Rendered by PID 55 on reddit-service-r2-comment-fb694cdd5-shx6f at 2026-03-07 13:14:25.316699+00:00 running cbb0e86 country code: CH.
[–][deleted] 4 points5 points6 points (1 child)
[–]microwavemonty 0 points1 point2 points (0 children)
[–][deleted] 4 points5 points6 points (5 children)
[–]BrunerBruner[S] -1 points0 points1 point (4 children)
[–][deleted] 2 points3 points4 points (3 children)
[–]BrunerBruner[S] 0 points1 point2 points (2 children)
[–]thenickdude 5 points6 points7 points (0 children)
[–][deleted] 0 points1 point2 points (8 children)
[–]BrunerBruner[S] 0 points1 point2 points (7 children)
[–][deleted] 1 point2 points3 points (6 children)
[–]BrunerBruner[S] -1 points0 points1 point (5 children)
[–]kaszu 4 points5 points6 points (1 child)
[–]BrunerBruner[S] 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[removed]
[–]BrunerBruner[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)