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

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

gnarf
  • 105,192
  • 25
  • 127
  • 161
vick
  • 939
  • 1
  • 8
  • 14

7 Answers7

139

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 key to value. If you want an array of the 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);
    });
    
  • objects in JavaScript are incredibly flexible. If you want to create an object {foo: 1}, all of the following work:

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

To wrap up, do you want this?

var data = {};
// either way of changing data will work:
data.pageNo = $(this).text();
data['sortBy'] = $("#sortBy").val();

$("#results").load("jquery-routing.php", data);
ojrac
  • 13,231
  • 6
  • 37
  • 39
  • thanks for all the help, I will make my next question simple, How can I pass two variables to my jquery? I want to pass PageNo and SortBy ... 2 ?? – vick Apr 16 '10 at 03:16
  • From what I understand: load(url, data) hits URL and sends data as the POST. If you wanted to hit routing.php?p=2&sortBy=error, you'd need to add that to the URL ("routing.php?p=" + pageNo + "&sortBy=" + sortBy). That, or make your PHP script read from $_POST or $_REQUEST instead of $_GET. – ojrac Apr 16 '10 at 04:26
  • 1
    Seems here `var data = {};` need to change to `var data = [];` – Andris Nov 29 '14 at 02:06
  • Isnt jQuery a wrapper around javascript, and not a "plugin"? – Ed DeGagne Dec 15 '16 at 20:38
  • Plugin is a weird word, yeah. I think I meant to say "library." – ojrac Dec 16 '16 at 17:17
29

You may be confusing Javascript arrays with PHP arrays. In PHP, arrays are very flexible. They can either be 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 consider these two to be different things, Javascript being among them. An array in Javascript is always numerically indexed:

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

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

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

They're not interchangeable. If you need "array keys", you need to use an object. If you don't, you make an array.


* Technically everything is an Object in Javascript, please put that aside for this argument. ;)

deceze
  • 510,633
  • 85
  • 743
  • 889
11

Not completely clear what you mean. Perhaps:

<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>
gnarf
  • 105,192
  • 25
  • 127
  • 161
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • 1
    Yes, but he's also coming from php, where arrays are also hashes... In JavaScript, its not a 'hash' either - its just an object. +1 - this was my interpretation of the question as well.. – gnarf Apr 16 '10 at 03:05
  • 2
    I know the difference between JavaScript arrays and objects. However, it seemed like the real point of the question was to construct the parameters dynamically. – Matthew Flaschen Apr 16 '10 at 03:17
8

Here is the 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
elixenide
  • 44,308
  • 16
  • 74
  • 100
Therichpost
  • 1,759
  • 2
  • 14
  • 19
1

I haven't been using jquery for a while but you might be looking for this:

jQuery.makeArray(obj)

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
1

Here is an example that I used.

<script>
  $(document).ready(function(){
      var array =  $.makeArray(document.getElementsByTagName(“p”));
      array.reverse(); 
      $(array).appendTo(document.body);
  });
</script>
JH_
  • 406
  • 1
  • 4
  • 15
Srikar Doddi
  • 15,499
  • 15
  • 65
  • 106
0

your question makes no sense. you are asking how to turn a hash into an array. You cant.

you can make a list of values, or make a list of keys, and neither of these have anything to do with jquery, this is pure javascript

mkoryak
  • 57,086
  • 61
  • 201
  • 257
  • ... in other words, an Array is an Array, a Hash is a Hash. Use whatever is appropriate for the situation, they're not interchangeable. – deceze Apr 16 '10 at 03:01