简体   繁体   English

如何为数组元素分配空值?

[英]How can I assign null value to an array element?

I am facing too small problem, could you give me idea how to solve that. 我面临的问题太小,您能给我个解决办法的想法吗?

for(var j=cArray.length-1;j>=0;j--)
{
  if(cArray[j]=='.') {
    cArray[j]='';
    break;
  }
  else{
    cArray[j]='';
  }
}

I wrote this for loop in javascript.NULL value is not assigning to array element. 我在javascript中编写了此for循环。NULL值未分配给数组元素。 At last i am getting what is the content in cArray[j] only.I can't able to change that value.My declaration is correct or not? 最后我只得到cArray [j]中的内容。我无法更改该值。我的声明正确与否?

What are you trying to accomplish? 你想达到什么目的?

What the code does in this form is that it makes all elements in an array '' (empty) that are after the last '.' 代码以这种形式执行的操作是,使数组'' (空)中的所有元素都位于最后一个'.'之后'.' element. 元件。

If you just want to truncate the array you could do somethink like this: 如果只想截断数组,可以执行以下操作:

var jsArray = ['H','e','l','l','o','.','w','o','r','l','d'];
jsArray.length = 5;
alert(jsArray.length); // returns 5

Your code is right. 您的代码是正确的。 Maybe it is empty? 也许是空的? See my demo and observe as it works =) 观看我的演示并观察它是否有效=)

To truncate the array at the first . 首先截断数组. :

for(var j=cArray.length-1;j>=0;j--)
{
    if(cArray[j]=='.') {
        cArray.length = j;
        break;
    }
}

Or, if the array is really just a string: 或者,如果数组实际上只是一个字符串:

var myString = "1.1.1";
var result = myString.split(".");
var firstPart = result[0];

firstPart now contains 1 . firstPart现在包含1

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

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