简体   繁体   中英

array.splice not working

i want to remove array element, but giving error while using splice,

im using following function with myAra as global var, but in console ,it is giving me an error, TypeError: myAra.splice is not a function

var myAra = Array();
function charCounts(e,textAreaId)
{
    myAra = $("#"+textAreaId).val();
    var countNewLines = stringOccurrences(myAra, "\n");


    if(myAra.length>75)
    {
        for (var i = 75; i >myAra.length; i++) 
        {
            myAra.splice(i, 1);

        }

        $("#"+textAreaId).val(myAra);
    }
}

myAra is a String , not an Array , at the point when you call splice . It has the value of the element.

This is a nice example of why globals are EVIL , sure you declared the variable an array (badly): var myAra = Array() (I'll explain at the end what's bad about this), but later on:

myAra = $("#"+textAreaId).val();//returns a string, variable is now a string, not an array

You've reassigned a string to the array, so the variable now references a string constant, and cannot be used as an Array (not safely, in a X-browser way at least).

Array() is bad, why? Well, for starters, you're calling a constructor, but you're not using the new keyword. With arrays that's not a big problem (it'll return a new instance all the same), but when you start defining your own objects, and constructors, you'll find yourself up to your neck in globals.
Also, suppose you wanted an array and initialize the first element to an int: var anArray = new Array(2); , you won't get an array that looks like this: anArray[0] === 2 , you'll get anArray === [undefined,undefined] . Compare that to var anArray('2') --> ['2']. Given the fact that JS is loosely typed, and you'll often use variables when initializing an array, it's hard to tell weather or not you're passing a numeric string or a number to the constructor.
The best way to initialize arrays is by using the literal notation: [2,3,4] , as an added bonus, it requires less typing, too

Replace the following:

if(myAra.length>75)
{
    for (var i = 75; i >myAra.length; i++) 
    {
        myAra.splice(i, 1);

    }

    $("#"+textAreaId).val(myAra);
}

with the below code:

if(myAra.length>75)
{
    var moreNum = myAra.length - 75;
    myAra.splice(75, moreNum ); // remove all items after the 75th item

    $("#"+textAreaId).val(myAra);
}

Note - splice change the actual array, that's why the loop was failing. Hope it helps.

You are assigning a string value directly to the myAra so it will convert it to string ..typeOf myAra. Use myAra[0]=$("#"+textAreaId).val();...since javascript is a loosely coupled language

In the first line you used var myAra = Array() , but the jQuery val() function returns a string.

EDIT: Also I think the prefered way of creating arrays in JS is the var myArray = [] , and not using the var myArray = new Array() expression.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM