78
$(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()}

2
  • 1
    Could you clarify? I don't think anyone really understood what you are trying to ask. Commented Apr 16, 2010 at 6:28
  • Jquery is not a language rather you should ask how to this in javascript. Commented Nov 18, 2015 at 6:00

7 Answers 7

141

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);
Sign up to request clarification or add additional context in comments.

5 Comments

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 ... <a href="routing.php?p=2&SortBy=error">2</a> ??
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.
Seems here var data = {}; need to change to var data = [];
Isnt jQuery a wrapper around javascript, and not a "plugin"?
Plugin is a weird word, yeah. I think I meant to say "library."
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. ;)

2 Comments

Malformed arrow function parameter list
Huh? It's not supposed to be Javascript, if you read attentively…
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>

2 Comments

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..
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.
9

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

Comments

1

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

jQuery.makeArray(obj)

Comments

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>

Comments

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

1 Comment

... in other words, an Array is an Array, a Hash is a Hash. Use whatever is appropriate for the situation, they're not interchangeable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.