1

I tried doing delete weapons[i]; but even do when I do weapons.length I still get 1. Even though it should be 0. How do I definitely remove an array from weapons[] array?

I comb over the weapons array by doing this:

        for (var i = 0, setsLen = weapons.length; i < setsLen; ++i ) {

             var searchWeapon = weapons[i].split("|");
             // console.log('['+i+'] >> Weapon ID: ' + searchWeapon[0] + ' | Y: ' + searchWeapon[1] + ' | X: ' + searchWeapon[2]);    
             if (searchWeapon[1] == Y && searchWeapon[2] == X) { 
                delete weapons[i];
             }           
            }

and I store each array as 3|10|4 where 3 is weapon ID, 10 is Y, and 4 is X.

6 Answers 6

3

Check out the .splice method

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/splice

Using your example:

for (var i = 0, setsLen = weapons.length; i < setsLen; ++i ) {
  var searchWeapon = weapons[i].split("|");
  if (searchWeapon[1] == Y && searchWeapon[2] == X) { 
    weapons.splice(i,1);
  }           
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for this. Splice did the trick. Also Splice was a horrible movie.
Link to MDC not W3FOOLS. Splice
3

Use splice

array.splice(index,howmany)

Comments

3

If your array doesn't need to be sorted, you could use array.pop() as it's extremely fast.

if(index == array.length-1) array.pop();
else array[index] = array.pop();

This method doesn't need to re-index everything after what you've splice()'d, so it remains quick whether your array has a length of 5 or 350,000.

3 Comments

Nice piece of code. But is this meaningfully faster than splice in a real-world context?
Yes.. A lot faster. Well, depending on the length of your array. pop() is always the same speed whereas splice gets slower with the length of the array.
No probs.. It's more something to keep in mind with game dev when you're listing 500-1000 projectiles and stuff haha.
2

Additional to the answers above: you should walk the array from the end to the start, otherwise you'll miss elements inside the loop if a item has been removed. Demo: http://jsfiddle.net/doktormolle/2f9Ye/

Comments

0

deleteing an array element leaves a hole behind with undefined left in the element's place. You probably want to use Array.splice.

var myArray = [1,2,3];
delete myArray[1]; // myArray is now [1, undefined, 3];

var myArray2 = [1,2,3];
myArray2.splice(1,1); // myArray2 is [1, 3]

Comments

0

To remove an element from an array use the splace method. Example:

var myArr = ["apple", "orange", "pear"];
alert(myArr.length); // 3
myArr.splice(1,1); // start at 1th index (i.e 2nd element), remove 1 element
alert(myArr.length); // 2
for (var i=0; i<myArr.length; i++) alert(myArr[i]); // "apple", "pear"

1 Comment

LOLs. Yes, of course: Array.prototype.splace = Array.prototype.splice;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.