4

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); 
        }
    }
}
Faarbhurtz
  • 550
  • 1
  • 8
  • 27
  • 1
    You can, but you can't do it with only one array. – rdodev Dec 09 '13 at 14:53
  • 1
    Why don't you just have a variable called `arraylength` that's incremented each time an element is added to the array but doesn't change when an element is removed. – Andy Dec 09 '13 at 14:54
  • Why don't you want use splice? http://stackoverflow.com/questions/500606/javascript-array-delete-elements – Jurik Dec 09 '13 at 14:54
  • The length of an array will always be the highest index + 1 (indices are zero based), so no, you can't remove something, keep the index and expect length to be accurate -> http://jsfiddle.net/vLDeA/ – adeneo Dec 09 '13 at 15:00
  • @adeneo: *"The length of an array will always be the highest index + 1"* That depends on what you mean by "highest index." It's entirely possible for `length` to be `7` when there is no entry at index `6`. `var a = []; a.length = 7; console.log(a.hasOwnProperty(6)); // false` – T.J. Crowder Dec 09 '13 at 15:04
  • @T.J.Crowder - Well, yes! If you specifically set the length property yourself you can set it to anything I suppose, I meant by just adding and removing with the built in methods. – adeneo Dec 09 '13 at 15:06
  • @adeneo guys; setting length = length-1 might also help, i'm not sure what the exact implications of this are though? – Faarbhurtz Dec 09 '13 at 15:08
  • @T.J.Crowder - Would be neat if there was something like enumerable = false for arrays? – adeneo Dec 09 '13 at 15:08
  • @adeneo: You mean like this? http://jsbin.com/IcemuJu/1/edit ;-) – T.J. Crowder Dec 09 '13 at 15:10
  • 1
    @T.J.Crowder - Indeed, as arrays are objects, defineProperty works for those as well, and enumerable is false by default when using that method. Quite clever, didn't think of that. – adeneo Dec 09 '13 at 15:16
  • What are you guys talking about, you are confusing me :o – Faarbhurtz Dec 09 '13 at 15:17
  • 1
    @Faarbhurtz Setting the length manually won't work, the deleted key will still be missing, and the last value will be chopped off. You end up with an array of length = originalLength - 1, but with originalLength - 2 keys. All your options are summarized in T.J. Crowder's answer. – bfavaretto Dec 09 '13 at 15:26

3 Answers3

4

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.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

This works perfectly.

    var delrow = window.event.srcElement;
    while ((delrow = delrow.parentElement) && delrow.tagName != "TR");
    delrow.parentElement.removeChild(delrow);
IMRUP
  • 1,463
  • 2
  • 11
  • 17
0
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]);
                            }
Faarbhurtz
  • 550
  • 1
  • 8
  • 27