How to create an array in jquery?

$(document).ready(function() { $("a").click(function() { $("#results").load("jquery-routing.php", { pageNo: $(this).text(), sortBy: $("#sortBy").val()} ); return false; }); }); 

How to create an array in jQuery and use this array instead of { pageNo: $(this).text(), sortBy: $("#sortBy").val()}

+53
javascript jquery arrays
Apr 16 '10 at 2:50
source share
7 answers

Some thoughts:

  • jQuery is a JavaScript library, not a language. So, JavaScript arrays look something like this:

     var someNumbers = [1, 2, 3, 4, 5]; 
  • { pageNo: $(this).text(), sortBy: $("#sortBy").val()} is a map of the key to the value. If you need an array of keys or values, you can do something like this:

     var keys = []; var values = []; var object = { pageNo: $(this).text(), sortBy: $("#sortBy").val()}; $.each(object, function(key, value) { keys.push(key); values.push(value); }); 
  • JavaScript objects are incredibly flexible. If you want to create an object {foo: 1} , all of the following works:

     var obj = {foo: 1}; var obj = {}; obj['foo'] = 1; var obj = {}; obj.foo = 1; 

To wrap you want it?

 var data = {}; // either way of changing data will work: data.pageNo = $(this).text(); data['sortBy'] = $("#sortBy").val(); $("#results").load("jquery-routing.php", data); 
+103
Apr 16 '10 at 3:01
source share

You can mislead Javascript arrays with PHP arrays. In PHP, arrays are very flexible. They can be either numerically indexed, or associative, or even mixed.

 array('Item 1', 'Item 2', 'Items 3') // numerically indexed array array('first' => 'Item 1', 'second' => 'Item 2') // associative array array('first' => 'Item 1', 'Item 2', 'third' => 'Item 3') 

Other languages ​​see these two as different things, including Javascript. An array in Javascript is always numerically indexed:

 ['Item 1', 'Item 2', 'Item 3'] // array (numerically indexed) 

An "associative array", also called a Hash or Map, technically an object in Javascript *, works like this:

 { first : 'Item 1', second : 'Item 2' } // object (aka "associative array") 

They are not interchangeable. If you need "array keys", you need to use an object. If you do not, you will create an array.




* Technically, this is all an object in Javascript, please postpone this for this argument .;)

+25
Apr 16 '10 at 3:10
source share

It’s not entirely clear what you mean. Maybe:

 <script type="text/javascript"> $(document).ready(function() { $("a").click(function() { var params = {}; params['pageNo'] = $(this).text(); params['sortBy'] = $("#sortBy").val(); $("#results").load( "jquery-routing.php", params ); return false; }); }); </script> 
+9
Apr 16 '10 at 2:54
source share

I haven't used jquery for a while, but you could find this:

jQuery.makeArray (obj)

+1
Apr 16 '10 at 2:51
source share

Here is a clear working example:

 //creating new array var custom_arr1 = []; //storing value in array custom_arr1.push("test"); custom_arr1.push("test1"); alert(custom_arr1); //output will be test,test1 
+1
Nov 06 '16 at 7:41
source share

Here is an example that I used.

 <script> $(document).ready(function( ) { var array = $.makeArray(document .getElementsByTagName("p")); array.reverse( ); $(array). appendTo(document.body); }); </script> 
0
Apr 16 2018-10-16T00:
source share

Your question does not make sense. you ask how to turn a hash into an array. You can’t .

you can make a list of values ​​or make a list of keys, and none of them has anything to do with jquery, this is pure javascript

0
Apr 16 '10 at 2:57
source share



All Articles