-1

I am dealing with an array that I want to delete an object from and return the new length of the array. For all other numbers, it works - but for one item, it does not. Not sure how to fix it so that the array length is 0 after the only object is deleted. My code is below:

Here's an example where I had one object in the 'player' array:

function deletecamera(id){
    alert('before the splice, player length =' + player.length); //returns 1

    delete player.splice((id),1);
    i=0;
    for (i=0;i<player.length;i++){
        player.id=i;
    }

    alert('after reassigning, player length =' + player.length); // still returns 1?!
    refreshlist();
}
George Cummins
  • 28,485
  • 8
  • 71
  • 90
itamar
  • 3,837
  • 5
  • 35
  • 60
  • 2
    why do you have `(id)` in the splice function? – karthikr Jun 07 '13 at 20:05
  • isn't the splice() doing the deletion? wtf is 'delete' doing there? is it from prototyping the code? cause that doesn't seem right! – DrCord Jun 07 '13 at 20:07
  • and your for loop with the player id , doesnt make sense either. if you want to assign the length of player -1 to player.id , just do player.id = player.length-1; – mpm Jun 07 '13 at 20:09
  • I should have clarified. id is a parameter given in the function for the delete button. Apologies if I offended you with my sloppiness. – itamar Jun 07 '13 at 20:13
  • it is not slopiness , your code just dont make sense , AT ALL ,if you give more context maybe we can help. what is player , what kind of data does it hold and what does your function is supposed to do with player, and what is id ? an index of player array ? a property of player ? – mpm Jun 07 '13 at 20:15
  • player was an array, and it contained objects. Adding a condition of if (player.length === 1){ player.splice(0,1); } helped. Sorry about the confusing code. JS is far from my strong suit. – itamar Jun 07 '13 at 20:18
  • If your button passes a `1`, `.splice(1,1)` would remove the 2nd element in the array since they are `0` based. Which in your case wouldn't remove anything. `delete` is also unnecessary, as `splice` already removes the element in question. – Chase Jun 07 '13 at 20:18
  • @ItamarOKestenbaum have you even looked at my answer before implementing more weird code? And seriously ... `.length === 1` ? – nl-x Jun 07 '13 at 20:25

4 Answers4

3

the delete keyword doesn't remove the object from the array, it sets its value to undefined, so the size of the array stay the same. See example here: http://jsfiddle.net/up5XX/

Antoine
  • 2,785
  • 1
  • 16
  • 23
  • anyway that code still doesnt even make sense with or without the delete. – mpm Jun 07 '13 at 20:12
  • 1
    The question was more about why the length of the array stays the same. – Antoine Jun 07 '13 at 20:14
  • doesnt change anything since delete doesnt affect the splice operation , only the result of the splice. – mpm Jun 07 '13 at 20:17
  • Delete turned out to be extraneous. I got it from what now seems to have been an incorrect forum post. player.splice() worked just fine. – itamar Jun 07 '13 at 20:25
2

If you want to remove the first element from the array player using .splice, you can do this:

player.splice(0, 1);
recursive
  • 83,943
  • 34
  • 151
  • 241
  • Thank you - this worked. I appreciate the help even with the shoddy code in my question! – itamar Jun 07 '13 at 20:19
  • No. he doesn't want to remove the first element. he want to remove the passed id! this will indeed clean out your array eventually , but wont do what it is supposed to. – nl-x Jun 07 '13 at 20:22
  • @nl-x: He seems satisfied, and removing the id would do nothing to reduce the array length, so I'm sticking with it. – recursive Jun 07 '13 at 20:33
  • @recursive yeah. in the comments to the OP he states he used this to only do this if the length is just 1... hinting he actually has no clue. :) but I do think youre right in thay the `.id` wont affect the `.length` :( – nl-x Jun 07 '13 at 20:49
0

yeah, thinking a bit more about this, I bet it will work if you change this line:

delete player.splice((id),1);

to

player.splice((id),1);
DrCord
  • 3,917
  • 3
  • 34
  • 47
  • 1
    also you can remove the extra parens around id. but this all will have no effect since it doesnt matter what you do with the results of the splice. whether you give it to delete, print, or some other thing. the function will have run... – nl-x Jun 07 '13 at 20:12
0

some weird codes there.

An array in JS is an object that can hold multiple [elements]. But just like with any other JS object you can add more members to it by just saying myArrayName.someMemberName = something. This will not be 'in' the array as if it was an element. This is even the JS poor mans way for an 'associative array’. This is what you are doing now with the .id = ...

You need to change

player.id = i;

into something like

player[i].id = i;

(or something like it. Don't know what the goal is there. I guess you want to reorder all Id's after deleting one in between.)

futhermore ... change the splice line to just this:

player.splice(id,1);

and remove the extra line with just:

i=0;

But I now realise these all are tips but no solution to your problem. Can you please add

alert('trying to remove id ' + id);

and confirm that you do at least once try to delete id 0 ?

nl-x
  • 11,762
  • 7
  • 33
  • 61
  • ill edit this one more time cause I dont seem to reach you, and Im quite sure this is your real problem – nl-x Jun 07 '13 at 20:33
  • I appreciate your help. This worked. JS is far from my strong suit - and I am not used to getting this negative a response on SO, so sorry for the delay in response. I swear, I'm new - not stupid :-) – itamar Jun 07 '13 at 20:49
  • i edited again to reflect the new insight. seems i dont have a definitive solution for you yet – nl-x Jun 07 '13 at 21:02
  • Thanks I appreciate the effort. I'll update my code in the meantime to reflect the correction and I'll keep working on it. If I come to a solution I'll post it here. Thanks! – itamar Jun 07 '13 at 21:12