473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to return an array from a Javascript function ?

296 100+
Hello,

How to return an array from a Javascript function ?

For eg:

Expand|Select|Wrap|Line Numbers
  1. function test()
  2. {
  3.     var test1 = document.getElementsByName("textarea");
  4.  
  5.     return test1;
  6. }
Please correct the above code.
May 29 '08 #1
22 5816
acoder
16,027 Expert Mod 8TB
If you want an array of the textareas on the page, change getElementsByName to getElementsByTagName.
May 29 '08 #2
pankajit09
296 100+
If you want an array of the textareas on the page, change getElementsByName to getElementsByTagName.

No I want to extract textareas which have a specific name.


Also tell me how to return associative arrays ?
May 29 '08 #3
acoder
16,027 Expert Mod 8TB
Then replace "textarea" in your original code to the name of the textareas.
May 29 '08 #4
pankajit09
296 100+
Then replace "textarea" in your original code to the name of the textareas.
That I am already doing .


Also tell me how to return an object.

For eg:

Expand|Select|Wrap|Line Numbers
  1. var obj = new Object(); 
  2. obj["firstName"]=”vijay”;
  3. obj["lastName"]=”khambalkar”;
May 29 '08 #5
acoder
16,027 Expert Mod 8TB
If it's an array, use [] in place of "new Object()" and then after those statements, return obj; will return an array.
May 29 '08 #6
pankajit09
296 100+
If it's an array, use [] in place of "new Object()" and then after those statements, return obj; will return an array.

No I want to know how to return associative arrays ?
May 29 '08 #7
acoder
16,027 Expert Mod 8TB
What does the following do?
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script>
  4. function test() {
  5.     var obj = [];
  6.     obj["firstName"]="vijay";
  7.     obj["lastName"]="khambalkar";
  8.     return obj;
  9. }
  10. </script>
  11. </head>
  12. <body>
  13. <script>
  14. var t = test();
  15. document.write(t["firstName"]);
  16. </script>
  17. </body>
  18. </html>
May 29 '08 #8
pankajit09
296 100+
What does the following do?
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script>
  4. function test() {
  5.     var obj = [];
  6.     obj["firstName"]="vijay";
  7.     obj["lastName"]="khambalkar";
  8.     return obj;
  9. }
  10. </script>
  11. </head>
  12. <body>
  13. <script>
  14. var t = test();
  15. document.write(t["firstName"]);
  16. </script>
  17. </body>
  18. </html>

I want to send the array to a PHP file using Ajax.

How to read the array in PHP ?
May 29 '08 #9
acoder
16,027 Expert Mod 8TB
I want to send the array to a PHP file using Ajax.

How to read the array in PHP ?
Now it makes more sense what you're trying to do.

Send it as a string with a unique delimiter and then split the string using that character with PHP.
May 29 '08 #10
pankajit09
296 100+
Now it makes more sense what you're trying to do.

Send it as a string with a unique delimiter and then split the string using that character with PHP.

For sending it as a string I have to traverse through the array, take each value and concatenate it.

Is there any other way ?
May 29 '08 #11
acoder
16,027 Expert Mod 8TB
Use the join() method to put all the elements of an array into a string.
May 29 '08 #12
pankajit09
296 100+
Use the join() method to put all the elements of an array into a string.
But in that case will I get the keys also ?

Its an associative array.
May 29 '08 #13
acoder
16,027 Expert Mod 8TB
Do you need the keys too?

Since it's an associative array/object, use a for...in loop:
Expand|Select|Wrap|Line Numbers
  1. var obj = new Object;
  2. obj["whatever"] = "something";
  3. ...
  4. var str = ""
  5. for (var i in obj)
  6.   str+=obj[i] + ":";
  7.  
May 29 '08 #14
pankajit09
296 100+
Do you need the keys too?

Since it's an associative array/object, use a for...in loop:
Expand|Select|Wrap|Line Numbers
  1. var obj = new Object;
  2. obj["whatever"] = "something";
  3. ...
  4. var str = ""
  5. for (var i in obj)
  6.   str+=obj[i] + ":";
  7.  

I know this way.

I wanted a way to return the associative array.
May 30 '08 #15
acoder
16,027 Expert Mod 8TB
If you need the keys too, add them to the string too, e.g.
Expand|Select|Wrap|Line Numbers
  1. for (var i in obj) {
  2.     str += i + ":" + obj + "|";
  3. }
May 30 '08 #16
hsriat
1,654 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. var aArray = new Array;
  2. aArray['key'] =  new Array;
  3. aArray['value'] =  new Array;
  4. var key;
  5. for(key in yourArray) {//yourArray is your original array
  6.     aArray['value'].push(yourArray[key]);
  7.     aArray['key'].push(key);
  8. }
Hope you could utilize aArray
May 30 '08 #17
pankajit09
296 100+
If you need the keys too, add them to the string too, e.g.
Expand|Select|Wrap|Line Numbers
  1. for (var i in obj) {
  2.     str += i + ":" + obj + "|";
  3. }

I don't want the values in a string.

I just want to return the associative array.
May 30 '08 #18
pankajit09
296 100+
Expand|Select|Wrap|Line Numbers
  1. var aArray = new Array;
  2. aArray['key'] =  new Array;
  3. aArray['value'] =  new Array;
  4. var key;
  5. for(key in yourArray) {//yourArray is your original array
  6.     aArray['value'].push(yourArray[key]);
  7.     aArray['key'].push(key);
  8. }
Hope you could utilize aArray

You are creating two arrays.

I want a single associative array.
May 30 '08 #19
hsriat
1,654 Expert 1GB
I don't want the values in a string.

I just want to return the associative array.
Tell me one thing first...
What is an associative array?
Expand|Select|Wrap|Line Numbers
  1. arr[0] = 'a'
  2. arr[1] = 'b'
  3. arr[2] = 'c'
  4. arr[3] = 'd'
  5. arr[4] = 'e'
If this is an array, what results do you want?
May 30 '08 #20
acoder
16,027 Expert Mod 8TB
I don't want the values in a string.

I just want to return the associative array.
We already did that earlier with the return statement. I thought you wanted to pass this to a PHP script via Ajax.
May 30 '08 #21
pankajit09
296 100+
Tell me one thing first...
What is an associative array?
Expand|Select|Wrap|Line Numbers
  1. arr[0] = 'a'
  2. arr[1] = 'b'
  3. arr[2] = 'c'
  4. arr[3] = 'd'
  5. arr[4] = 'e'
If this is an array, what results do you want?

Actually I want to send an associative array to a PHP file via Ajax without converting it to a string.

arr["asasa"] = 'a'
arr["wew"] = 'b'
arr["rtr"] = 'c'
May 30 '08 #22
hsriat
1,654 Expert 1GB
Okey... if that is the case, you have to send it as a string only.
In PHP, you can use explode to convert your string back to an array.

Use what acoder suggested in post#16.
Then when your variables is received by your PHP script.
Use:[php]$your_final_array("key"=>array(), "value"=>array());
$your_array = explode(",", $_POST['your_array_string']); //or GET... which ever you using
foreach ($your_array as $each_value)
{
array_push($your_final_array['key'], strtok($each_value, ":"));
array_push($your_final_array['value'],strtok(":"));
}[/php]
May 30 '08 #23

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Don | last post by:
I want the server-side php script to return a browser page that is essentially a copy of the original client page that contained the <form> which referenced the php script in the first place....
0
by: Mike Copeland | last post by:
In the following code I am calculating 2 values when the "Compute Distance" button is pressed. I would like to find a way to "return" both value to the form so I can show both when the calculation...
17
by: strout | last post by:
function F(e) { return function(){P(e)} } Can anybody tell me what the code is doing? If return another function all in a function I would do function F(e)
5
by: Andrew Poulos | last post by:
If I'm searching for an occurance of a value in a multi-dimensional array how can I get it's index returned as an array, if found? For example, if: foo = new Array(); foo = , 5, , 9, 10]; ...
2
by: BrianP | last post by:
Hi, I have had to invent a work-around to get past what looks like a JavaScript bug, the malfunctioning Perl-like JavaScript array functions including SPLICE() and UNSHIFT(). I have boiled it...
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
4
by: mflll | last post by:
I am looking into the different techniques of handling arrays of edit boxes in Java Script. The first program below works fine. However, are there better ways of doing this, where the person...
10
by: SM | last post by:
Hello I'm trying to create a multi dimensional array in JavaScript, but after some reading i still can't figure out how to apply it to my model. Here it is: I have a list A and for each item...
6
by: =?Utf-8?B?Sm9obiBCdW5keQ==?= | last post by:
Hey guys, I've searched high and low for a way to populate a vb array with data from a javascript array. I can find 50 ways to do it with ASP but not one for VB. I appreciate what help you can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.