开发者

How can I find the length of a specific index in an array

have tried various things

split[6].length
String.split[6].length

along these lines without success get this error message for the last one ...

ReferenceError: "string" is not defined.

Hi Thanks for all the replies, in the end I created an array based on the index of the original array and then queried the length of that. As you can see I am having trouble removing single and double quotes from the input strings. New to javascript and its making me a little crazy lol.

// Loop through all the input messages
for (var i = 0; i < input.length; i++) {    
var next = output.append(input[i]);
// Get the body of the current input message
var body = input[i].text;
// Set the body
next.text = body ;
next.text.replace(/\'/g, "&#39;");
next.text.replace(/\"/g, "&#34;");
//replace(/['"]/g,'');
// Set a property    
var split = next.text.split(",");       
var array1 = split[5];
var array2 = split[2];
next.setProperty("aaaLength", split.length);
next.setProperty("开发者_如何转开发aaaSplitValue", split.length);  
next.setProperty("aaaArray1Value", split.length); 
next.setProperty("aaaArray2Value", split.length);

if (next.getProperty("BaseFilename")=="name"){
    next.text.replace(/\'/g, "&#39;");
    next.text.replace(/\"/g, "&#34;");
    //replace(/['"]/g,'');
    if(split.length>10){

        next.setProperty("FullFilename","nameError"+i);
        next.setProperty("BaseFilename","nameError"+i);
        next.setProperty("Suffix",".err");

        }
        if(array1.length>10){
            next.setProperty("FullFilename","nameSnameSuffixError"+i);
            next.setProperty("BaseFilename","nameSnameSuffixError"+i);
            next.setProperty("Suffix",".err");      
        }
}


Length should work if the elements are strings. See the following in action at http://jsfiddle.net/46nJw/

var parts = "foo,bar,baz,foop".split(/,/);
alert( parts[3].length ); // should alert 4


var arr = ['one','two','three']

arr[1].length

returns 3


Are you sure it is returning a string?

You can force it to convert to a string like so:

String(split[6]).length;


I don't know what you need, so I give you all the options I can think of:

var commaSeparatedString = "one, two, three";
var str = commaSeparatedString.split(",");
alert (str.length) // outputs '3'
alert (str[2]); // outputs 'three'
alert (str[2].length); //outputs '5' 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜