By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,291 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

How to return an array from a Javascript function ?

100+
P: 296
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
Share this Question
Share on Google+
22 Replies


acoder
Expert Mod 15k+
P: 16,027
If you want an array of the textareas on the page, change getElementsByName to getElementsByTagName.
May 29 '08 #2

100+
P: 296
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
Expert Mod 15k+
P: 16,027
Then replace "textarea" in your original code to the name of the textareas.
May 29 '08 #4

100+
P: 296
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
Expert Mod 15k+
P: 16,027
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

100+
P: 296
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
Expert Mod 15k+
P: 16,027
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

100+
P: 296
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
Expert Mod 15k+
P: 16,027
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

100+
P: 296
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
Expert Mod 15k+
P: 16,027
Use the join() method to put all the elements of an array into a string.
May 29 '08 #12

100+
P: 296
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
Expert Mod 15k+
P: 16,027
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

100+
P: 296
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
Expert Mod 15k+
P: 16,027
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
Expert 100+
P: 1,654
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

100+
P: 296
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

100+
P: 296
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
Expert 100+
P: 1,654
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
Expert Mod 15k+
P: 16,027
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

100+
P: 296
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
Expert 100+
P: 1,654
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

Post your reply

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