• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

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

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
pborregg
Asked:
pborregg
  • 7
  • 5
1 Solution
 
Gurvinder Pal SinghCommented:
it is an empty array
0
 
StingRaYCommented:
It is returning an empty array;
0
 
pborreggAuthor Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
pborreggAuthor Commented:
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
 
pborreggAuthor Commented:
So, consequently, can I display the elements of the array in an alert() box if I return something other than an empty array?
0
 
StingRaYCommented:
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
 
pborreggAuthor Commented:
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
 
pborreggAuthor Commented:
I ran your code and got and error: "OUT OF STACK SPACE". Weird huh?
0
 
StingRaYCommented:
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
 
StingRaYCommented:
How do you put my EXAMPLE code in?
0
 
pborreggAuthor Commented:
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
 
pborreggAuthor Commented:
This actually works and works well!
0
 
StingRaYCommented:
You're welcome. :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now