0

I have a very strange problem with js console in chrome, if i go in chrome console and write :

var numero = new Array(["/php/.svn/tmp", "/php/.svn/props"]);

return me "undefined" so i think numero is an array with 2 elements, but if i write:

numero

returns:

[Array[2]]

after

numero.length

and return 1 ..... why? don't return 2 ??? where am I doing wrong? can i give a method that returns 2? thanks in advance

EDIT: I will explain my problem. I have a function that return this when i selected 2 items :

myFolders.getSelected()
["/php/.svn", "/php/upload.php"]

and this when selected one items:

myFolders.getSelected()
"/php/upload.php"

as u note the second one isn't an array.

now i use this method to activate on change selected item an calculate a global variable:

function calcoloNumeroElementi(){
    var numero = new Array(myFolders.getSelected());
    numeroElementiSelezionati = numero[0].length;
}

but returns always 1 or the number of characters when i selected only one items.

2
  • Don't do new Array(['fdsf','dfsfd']);, leave out the [ and ], just do new Array('sdfdsfsd','fsdfsdfs'); instead Commented Aug 26, 2013 at 17:09
  • Your .getSelected() method is returning an array; no reason to wrap it's return value in new Array(). Commented Aug 26, 2013 at 17:41

3 Answers 3

5

You're creating an array inside other array, that's why it returns 1.

console.log( numero[0].length ); // 2

So it should be:

var numero = ["/php/.svn/tmp", "/php/.svn/props"];

or

var numero = new Array("/php/.svn/tmp", "/php/.svn/props"); // without `[` and `]`

Then use console.log( numero.length );

Sign up to request clarification or add additional context in comments.

2 Comments

work like a claim for 2 elements: return 2 but with only one elements array dont work .... return the number of chararpters
@r1si Use console.log( numero.length ); after the changes.
0

Don't use New Array, use just literal notation:

var numero = ["/php/.svn/tmp", "/php/.svn/props"];

Update (Based on your comments)

If you have your function myFolders.getSelected() that returns a single string and you want to add it to array, you can do this either declaratively:

var numero = [myFolders.getSelected()]

Or, if you plan to add multiple values, e.g. in a loop, you can push new value into array

var numero = [];
...
numero.push(myFolders.getSelected());

3 Comments

yes but try this var numero = myFolders.getSelected(); undefined numero "/php/.svn/tmp" numero.length 13
What's myFolders.getSelected(); ? If it returns String, then "numero.length" will return that String's length in characters, this time Arrays are not involved
@r1si added possible code variation based on assumption that myFolders.getSelected() returns a string. If it returns other types, please specify
0

["/php/.svn/tmp", "/php/.svn/props"] returns an array containing the two strings.

new Array(arg0, arg1 ... argn); returns an array with the elements defined as the arguments

new Array(["/php/.svn/tmp", "/php/.svn/props"]); will return an array where the first elements is and array of two strings.

Try instead numero[0].length and see what you get.

Or istead define your array just like this var numero = ["/php/.svn/tmp", "/php/.svn/props"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.