how do I create an array in jquery?

JavascriptJqueryArrays

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

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);

Solution 2 - Javascript

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. ;)

Solution 3 - Javascript

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>

Solution 4 - Javascript

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

Solution 5 - Javascript

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

jQuery.makeArray(obj)

Solution 6 - Javascript

Here is an example that I used.

<script>
  $(document).ready(function(){
      var array =  $.makeArray(document.getElementsByTagName(“p”));
      array.reverse(); 
      $(array).appendTo(document.body);
  });
</script>

Solution 7 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvickView Question on Stackoverflow
Solution 1 - JavascriptojracView Answer on Stackoverflow
Solution 2 - JavascriptdecezeView Answer on Stackoverflow
Solution 3 - JavascriptMatthew FlaschenView Answer on Stackoverflow
Solution 4 - JavascriptTherichpostView Answer on Stackoverflow
Solution 5 - JavascriptShimmy WeitzhandlerView Answer on Stackoverflow
Solution 6 - JavascriptSrikar DoddiView Answer on Stackoverflow
Solution 7 - JavascriptmkoryakView Answer on Stackoverflow