Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138307

Is it possible to empty an array of null values but keep the keys unchanged in JavaScript?

$
0
0

I have an array that is constructed like:

enter image description here

Currently, all the solutions I've tried are similar, such as:

            this.quantities = this.quantities.filter(e => e.length);

These however goes ahead and deletes out all the empty arrays (as desired) and re-keys the array down, as is normal behaviour (ant not desired), so 500, 1000, 5000 becomes 1, 2 & 3.

Is there a way to cull the empty arrays without re-keying? Because I am feeling the memory issues, especially as we get into a higher range.

Here's a snippet of the front-end view for the project:

this.quantities array

I understand that I can just refactor my code to use a reference object - but as is the nature with business - I don't have a lot of time to spare refactoring and lookups by these quantity keys are woven throughout the project.

I am using VueJS & this is my method:

this.quantities[ this.enter_qty ] = [];
    this.quantities[ this.enter_qty ][ 0 ] = {
    quantity: this.enter_qty,
    item_description: null,
    supplier_contact_id: null
};
//this.quantities = this.quantities.filter(e => e.length);

Viewing all articles
Browse latest Browse all 138307

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>