简体   繁体   English

array.splice无法正常工作

[英]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 我使用以下函数将myAra用作全局变量,但是在控制台中,它给我一个错误,TypeError:myAra.splice不是函数

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 . 在调用splice时, myAra是一个String而不是一个Array 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: 这是一个很好的例子,说明为什么全局变量是EVIL ,请确保var myAra = Array()变量声明为数组(严重): var myAra = Array() (我将在最后解释这有什么不好),但稍后:

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). 您已将字符串重新分配给数组,因此该变量现在引用了字符串常量,并且不能用作数组(至少在X浏览器中是不安全的)。

Array() is bad, why? Array()不好,为什么? Well, for starters, you're calling a constructor, but you're not using the new keyword. 好吧,对于初学者来说,您正在调用构造函数,但没有使用new关键字。 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); 另外,假设您需要一个数组并将第一个元素初始化为一个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] . ,您将不会获得如下所示的数组: anArray[0] === 2 ,您将获得anArray === [undefined,undefined] Compare that to var anArray('2') --> ['2']. 将其与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. 鉴于JS的类型是松散的,并且在初始化数组时经常使用变量,因此很难判断天气是否将数字字符串或数字传递给构造函数。
The best way to initialize arrays is by using the literal notation: [2,3,4] , as an added bonus, it requires less typing, too 初始化数组的最佳方法是使用文字符号: [2,3,4]作为额外的好处,它也需要较少的键入操作

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. 注意 - splice更改实际的数组,这就是循环失败的原因。 Hope it helps. 希望能帮助到你。

You are assigning a string value directly to the myAra so it will convert it to string ..typeOf myAra. 您直接将字符串值分配给myAra,以便它将其转换为字符串..typeOf myAra。 Use myAra[0]=$("#"+textAreaId).val();...since javascript is a loosely coupled language 使用myAra [0] = $(“#” + textAreaId).val(); ...因为javascript是一种松散耦合的语言

In the first line you used var myAra = Array() , but the jQuery val() function returns a string. 在第一行中,您使用var myAra = Array() ,但是jQuery val()函数返回一个字符串。

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. 编辑:另外,我认为在JS中创建数组的首选方法是var myArray = [] ,而不使用var myArray = new Array()表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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