#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    12
    Rep Power
    0

    Splice() method for first element in array?


    Hello, I'm experiencing an odd issue using splice:

    Here's what's up:

    Code:
    var fruits = ["Banana", "Oklahoma"];
    fruits.splice(0,1);
    > ["Banana"]
    splice should start at index 0, delete 1 item - in this case, "Banana" - however, it removes "Oklahoma".

    Furthermore:

    Code:
    var fruits = ["Banana"];
    fruits.splice(0,1);
    > ["Banana"]
    Just like before, it should return an empty array.

    In my scenario, I need to be able to remove an arbitrary element from an array, so using something like shift() seems problematic. Surely Javascript has an easy way to do this?

    Thanks for the help!
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1045
    Hi,

    check the manual.

    The return value of splice() is what gets deleted, so that's not what you want. If you look at the original array, you'll see that the first element has been removed correctly.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo