Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Puzzling javascript array behavior

So, this may be a really stupid question, but I'm obviously missing something here.

Consider the following code:

    var selectedItems = [];
    selectedItems.push("0ce49e98-a8aa-46ad-bc25-3a49d475e9d3");
    //fyi, selectedItems[selectedItems.length] = "0ce49e98-a8aa-46ad-bc25-3a49d475e9d3"; produced the same result.

At the end selectedItems content looks like this:

Name              Value                                    Type
-------------     --------------------------------------   ------
selectedItems     {...}                                    Object
   -  [0]         "0ce49e98-a8aa-46ad-bc25-3a49d475e9d3"   String
   -  length      1                                        Long

But if I just try to call split() on the same string, like this:

selectedItems = "0ce49e98-a8aa-46ad-bc25-3a49d475e9d3".split(",")

Now the content of my supposed array looks like this (missing length):

Name              Value                                    Type
-------------     --------------------------------------   ------
selectedItems     {...}                                    Object
   -  [0]         "0ce49e98-a8aa-46ad-bc25-3a49d475e9d3"   String

Any idea what the difference is? What's actually happening here?
Thanks in advance.

UPDATED: I have a feeling there's actually something structurally different about the two resulting values, because (atlas) ajax chokes on the one with the length property when I try to pass it to a server-side WebMethod (no actual error message, but I know the call fails). I'm not sure.

UPDATE #2 I noticed that setting the targetLocationIdList this way, results in no 'length' property being displayed in Quick Watch window:

  var params = 
  {
    jobId : args.get_JobId(), 
    targetLocationIdList : retVal.split(',')
  };

But this results contain 'length' property displayed in Quick Watch window:

  var retValArr = [];
  retValArr = retVal.split(',');

  var params = 
  {
    jobId : args.get_JobId(), 
    targetLocationIdList : retValArr 
  };
like image 210
Kon Avatar asked Jun 13 '26 17:06

Kon


2 Answers

There isn't a difference at all programmatically. If you run your example in both chrome developers window and firebug it looks like the 2nd

 Name              Value               
 Type
 -------------     --------------------------------------   ------ 
 selectedItems     {...}                                    Object
    -  [0]         "0ce49e98-a8aa-46ad-bc25-3a49d475e9d3"   String

Length is an implied property

EDIT

var retVal = 'test';
var params = 
  {
    jobId : 1, 
    targetLocationIdList : retVal.split(',')
  }; 
console.log(params.targetLocationIdList.length) // prints 1

The code above prints 1 in IE8,Firefox,Chrome (in their dev tools or firebug) so think that this must be an issue with Visual Studio or with Atlas in the way that it shows the object.

like image 79
AutomatedTester Avatar answered Jun 15 '26 05:06

AutomatedTester


Might this be a bug in the debugger? (Or is this causing problems in the browser?)

like image 38
edeverett Avatar answered Jun 15 '26 05:06

edeverett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!