/    Sign up×
Community /Pin to ProfileBookmark

How to: Store a Collection in a JS Array ?

Hello There! Good day to everyone reading my post!
It seems that I need all the help I can get from you guys.

May I ask a question?

I need to know how can I store a java Collection in a JavaScript variable, more specifically a JavaScript Array Object type.

For example…

I have a Java Object that belongs to the class java.util.Collection, I put this [B]Collection[/B] in Session. so that I can at any time access it via

[CODE]request.getSession().getAttribute(“Collection”);[/CODE]

[I]I need to know how I can iterate that Collection using JavaScript?[/I]
or in other words, [I]How can I Store that Collection inside a JavaScript Array so that I can iterate it later?[/I]

Could anybody help me?
I would like to have some code examples if possible, I have run out of clues with my basic knowledge of JS.

Thanks in advance.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERMay 02.2007 — You don't specify what the delimiting character(s) are in the returned request,

but assuming it to be "n".

[code=php]
returned_request = request.getSession().getAttribute("Collection");
var Collection = new Array();
Collection = returned_request.split("n");

for (var i=0; i<Collection.length; i++) {
alert('request: '+i+' = '+Collection[i]);
}
[/code]


Modify to your heart's content.
Copy linkTweet thisAlerts:
@J_LoganauthorMay 02.2007 — Hello Sir,

Thank you for the reply!

Do you know if this could be applied to a JSP page in a similar way?

and by the way I don't understand what are the delimiting characters you just mentioned.

For what I know a java Collection can contain a number of Objects of almost any kind with no need of ending each Object added with a special character,

for example.

in java,
[CODE]Collection col = new ArrayList();
col.add("String 1");
col.add("String 2");
col.add("String 3");
//then I put the collection in session
request.getSession().setAttribute("Collection", col);[/CODE]


Thank you once more for the reply...
Copy linkTweet thisAlerts:
@JMRKERMay 02.2007 — I don't know enough of java to give you an intellegent answer here.

What I do know is that a delimiter is any character or group of characters

that is used to separate things that are grouped together.

For example: A paragraph is a group of sentences that are separated by a '.'

Or: A sentence is a group of words seperated by a space ' '.

In your example, "Collection" or 'col' is an array of strings.

When you use the col.add function to add to this arrayList,

you are creating a grouping of strings ('String 1", etc.) in an array.

You normally acquire each element in the arrayList via an index value [0...n]

In JS you could combine that list like: CollectionString = col.join('|')

where the delimiting character is the piping symbol ('|').

You could break the 'join'ed string apart with col_array = col.split('|')

which would create an array of elements in JS.

But as I have said, I don't know how the java function of request.getSession() works,

so this explaination may not be of benefit to you if you don't know the delimiter is.
Copy linkTweet thisAlerts:
@J_LoganauthorMay 02.2007 — Hello Sir,

Now I get it, that there is a special character that concatenates or joins Objects inside a Collection in a implicit way.

Now the question is if I could use the code you posted in the previous reply similarly in a JSP page?

Thanks for your time. I truly appreciate it.
Copy linkTweet thisAlerts:
@JMRKERMay 03.2007 — As I said before, I'm not very java literate (just an extreme novice here about JSP).

I don't see why it would not work, so give it a try and see what happens.

If that doesn't help, try asking the question on the 'java' forum if you don't get further helpful responses from the 'javascript' forum.

Let us know what happens. Good luck!
Copy linkTweet thisAlerts:
@J_LoganauthorMay 03.2007 — Hello,

I posted in the Java Forum, but my message has been completely Ignored, just so you know...and I haven't had the time to try out the solution you posted.

I'll tell you the results once I give it a shot.

Thanks for everything.
×

Success!

Help @J_Logan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...