Faarbhurtz
Faarbhurtz

Reputation: 580

How to remove an array element without splice and without undefined remaining?

Is it possible to remove an array element at a certain position, without rearranging indexes, and without that position changing to undefined?

I don't think that is possible with delete nor splice?

I need an accurate way to view the length of the array, without rearranging indexes.

I do not want to use splice because i have an object that has specific positions mapped to actual X,Y points of a tabel (Punkt).

UPDATE: actually, knowing if the array element exists out of ONLY undefined values might also help me, is there an easier way then looping through?

var keys = Object.keys(racks);
for (var i = 0; i < keys.length; i++)
{
    for (var x = 0; x < racks[keys[i]].punkt.length; x++)
    {
        if(racks[keys[i]].punkt[x].y == fullName)
        {
            //delete racks[keys[i]].punkt[x];
            racks[keys[i]].punkt.splice(x,1);
            console.log(keys[i] + " : " + racks[keys[i]].punkt.length); 
        }
    }
}

Upvotes: 4

Views: 2392

Answers (3)

IMRUP
IMRUP

Reputation: 1513

This works perfectly.

    var delrow = window.event.srcElement;
    while ((delrow = delrow.parentElement) && delrow.tagName != "TR");
    delrow.parentElement.removeChild(delrow);

Upvotes: 1

Faarbhurtz
Faarbhurtz

Reputation: 580

var punten = racks[keys[i]].punkt.length;
                            if(racks[keys[i]].punkt[x].y == fullName)
                            {
                                delete racks[keys[i]].punkt[x];
                                punten--;
                            }

                            if(punten==0)
                            {
                                console.log("Could have removed device: " + keys[i]);
                            }

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074248

I don't think that is possible with delete nor splice?

I need an accurate way to view the length of the array, without rearranging indexes.

Then delete, a hasOwnProperty or in guard when retrieving from the array, and a loop counting the elements (or a separate variable keeping track) is the only way to do it. JavaScript's standard arrays are inherently sparse (because they're not really arrays at all), they can have gaps in them where they don't have entries. To create a gap, delete the array entry using delete.

Example:

// Setup
var a = ["a", "b", "c", "d"];
console.log(a.length); // 4

// Using delete
delete a[2];           // Delete the entry containing "c"
console.log(a.length); // Still 4
a.hasOwnProperty(2);   // false

// Using the guard when getting an entry
if (a.hasOwnProperty(2)) { // Or `if (2 in a)`
    // Get and use [2]
}
else {
    // Do whatever it is you want to do when the array doesn't have the entry
}

// Finding out how many it *really* has:
var key;
var count = 0;
for (key in a) {
    if (a.hasOwnProperty(key)  &&        // See below
        /^0$|^[1-9]\d*$/.test(key) &&
        key <= 4294967294
        ) {
        ++count;
    }
}
console.log(count);    // 3

See this other answer for the details behind that if in the loop. If you never put non-element properties on the array, you can skip the second and third parts of that if.

Upvotes: 4

Related Questions