开发者

How can I assign null value to an array element?

开发者 https://www.devze.com 2023-02-07 09:50 出处:网络
I am facing too small problem, could you give me idea how to solve that. for(var j=cArray.length-1;j>=0;j--)

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. 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消