Solved

What does "return [ ];" mean using JavaScript?

Posted on 2012-03-27
13
228 Views
Last Modified: 2012-03-27
Here's a function:

function getChosenDocs()
{
 
 
  var listview = getListView();
 
  if (!listview || !listview.getSelectedCount())
   
      return [];
 
  var documents = [];
 
  for (var item = listview.item(0); item.exists(); item.gotoNext())
    if (item.getSelected())
      documents.push({Id: item.getAttribute("Id"), library: selectedLibrary.name});
        return documents;
}

I have NEVER seen a variable equal to just brackets [].  When I place an alert() or try catch, the return is "object [Object]".

Can someone please explain this...

Thanks,
0
Comment
Question by:pborregg
  • 7
  • 5
13 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37772688
it is an empty array
0
 
LVL 15

Expert Comment

by:StingRaY
ID: 37772690
It is returning an empty array;
0
 

Author Comment

by:pborregg
ID: 37772708
So what's the point?  I'm trying to capture the documents that have been selected in a listview. But if it's empty, what's the point, right?
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:pborregg
ID: 37772716
Or is this the part that I really should concern myself with?

  for (var item = listview.item(0); item.exists(); item.gotoNext())
    if (item.getSelected())
      documents.push({Id: item.getAttribute("Id"), library: selectedLibrary.name});
        return documents;
0
 

Author Comment

by:pborregg
ID: 37772721
So, consequently, can I display the elements of the array in an alert() box if I return something other than an empty array?
0
 
LVL 15

Accepted Solution

by:
StingRaY earned 500 total points
ID: 37772741
The point is the returned thing. When you get the returned array, you can loop through the elements inside. The elements are objects of Id and library. When no listview is specified, an empty array is returned to keep the iteration simple.

for example:

var docs = getChosenDocs();
for (x in docs) {
    alert(docs[x].Id);
}

Open in new window


from the code above, you can apply to the returned thing even there is no listview available.
0
 

Author Comment

by:pborregg
ID: 37772762
OK, great... I'm beginning to understand. Now, in all my years of writing code, I'm surprised, I've never seen anything like this:

for (var item = listview.item(0); item.exists(); item.gotoNext())

I thought it was like this:

for(var i=0;i <= [something];i++)
0
 

Author Comment

by:pborregg
ID: 37772781
I ran your code and got and error: "OUT OF STACK SPACE". Weird huh?
0
 
LVL 15

Expert Comment

by:StingRaY
ID: 37772797
Because of getListView() returns an object which let you iterate through its items by .gotoNext().

The for loop below is just a simple structure.
for(var i=0;i <= [something];i++)

Open in new window


The FOR-LOOP syntax is
for(<initialization>;<condition>;<increment/step>)

Open in new window


From "for (var item = listview.item(0); item.exists(); item.gotoNext())", you will get
Initialization = "var item = listview.item(0)"
Condition = "item.exists()"
Increment/Step = "item.gotoNext()"
0
 
LVL 15

Expert Comment

by:StingRaY
ID: 37772813
How do you put my EXAMPLE code in?
0
 

Author Comment

by:pborregg
ID: 37772837
I WORKED!!!! YEAH. It was my fault. I renamed the code here for security reasons, but when I put back the original names of the function calls, I got back EXACTLY what I needed. You da man!

Thanks and you got 500 pts.

Peter
0
 

Author Closing Comment

by:pborregg
ID: 37772841
This actually works and works well!
0
 
LVL 15

Expert Comment

by:StingRaY
ID: 37774816
You're welcome. :)
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delay Form Submission without touching existing code? 5 23
HTML5 Get data in table rows 5 29
HTML5 frame 5 25
Simple Total function not working 8 30
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question