Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I 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 do I create an array in jQuery and use that array instead of { pageNo: $(this).text(), sortBy: $("#sortBy").val()}

like image 368
vick Avatar asked Apr 16 '10 02:04

vick


People also ask

How can you use array with jQuery?

each function to iterate over elements of arrays, as well as properties of objects. The jquery . each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time.

What is an array in jQuery?

jQuery array is used to store multiple objects/values in a single array variable. jQuery is a compact and fast JavaScript library. It is rich with lots of new enhanced features. It simplifies HTML document traversal and manipulation, animation, event handling, and Ajax.

How do you create an array?

You can make an array of int s, double s, or any other type, but all the values in an array must have the same type. To create an array, you have to declare a variable with an array type and then create the array itself. Array types look like other Java types, except they are followed by square brackets ( [] ).

Can you create an array in JavaScript?

Creating an ArrayUsing an array literal is the easiest way to create a JavaScript Array. Syntax: const array_name = [item1, item2, ...]; It is a common practice to declare arrays with the const keyword.


1 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 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); 
like image 108
ojrac Avatar answered Oct 06 '22 00:10

ojrac