<p>I received an interesting issue about JsonDB not deleting correctly the index in an Array:<br />
[github-issue username=&#8221;belphemur&#8221; repository=&#8221;node-json-db&#8221; number=&#8221;10&#8243;]
<p>I thought using the <em>delete</em> keyword on an index in an array was deleting it (rendering its value undefined). In fact, it only set the index value to <em>NULL</em>.</p>
<h1>Don&#8217;t do</h1>
<pre class="brush: jscript; title: ; notranslate" title=""> 
 
var test = {array:&#x5B;1,2,3]}; 
 
delete test.array&#x5B;1]; 
 
console.log(test.array); //&#x5B;1, , 3] 
 
</pre>
<h1>Do</h1>
<pre class="brush: jscript; title: ; notranslate" title=""> 
 
var test = {array:&#x5B;1,2,3]}; 
 
test.array.splice(1,1); //Remove from index 1, 1 item. 
 
console.log(test.array); //&#x5B;1, 3] 
</pre>
<h1>Conclusion</h1>
<p>If you want to remove also the index from the array, use the <em>splice</em>. If you want to keep your index table intact, but remove only the value, use <em>delete</em>.</p>

Leave a Reply Cancel reply