0

I have a basic setup for my data like this:

myObj = {};
myObj[3] = {};
myObj[3].stuff = 'hello';
myObj[4] = {};
myObj[4].stuff = 'hello';

Now at some point i want to remove [4] from myObj. Currently i set it to false but this still exists in myObj. Like this Object {4: false}.

The issue here is if i want to loop myObj it will still loop the 4 which i don't want it to do.

How can I setup my data correctly so I can remove a given object and thus won't be iterated in a for loop.

Example of my loop is:

var i = 0;
for(var j in myObj){
  i++;
}
console.log(i); // = 2

Is there no way to rid of [4] so the total is then only 1?

Sir
  • 8,135
  • 17
  • 83
  • 146

1 Answers1

3

Use the delete operator:

delete myObj[4];
pdoherty926
  • 9,895
  • 4
  • 37
  • 68