Javascript 如何在 jquery 中创建数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2650277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:19:01  来源:igfitidea点击:

how do I create an array in jquery?

javascriptjqueryarrays

提问by vick

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

如何在 jQuery 中创建一个数组并使用该数组而不是 { pageNo: $(this).text(), sortBy: $("#sortBy").val()}

回答by ojrac

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;
    
  • jQuery 是一个 JavaScript 库,而不是一种语言。所以,JavaScript 数组看起来像这样:

    var someNumbers = [1, 2, 3, 4, 5];
    
  • { pageNo: $(this).text(), sortBy: $("#sortBy").val()}是键值映射。如果你想要一个键或值的数组,你可以这样做:

    var keys = [];
    var values = [];
    
    var object = { pageNo: $(this).text(), sortBy: $("#sortBy").val()};
    $.each(object, function(key, value) {
        keys.push(key);
        values.push(value);
    });
    
  • JavaScript 中的对象非常灵活。如果你想创建一个对象{foo: 1},以下所有的工作:

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

回答by deceze

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.

您可能会将 Javascript 数组与 PHP 数组混淆。在 PHP 中,数组非常灵活。它们可以是数字索引或关联的,甚至可以混合。

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:

其他语言认为这两者是不同的,Javascript 就是其中之一。Javascript 中的数组总是用数字索引:

['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:

“关联数组”,也称为 Hash 或 Map,技术上是 Javascript* 中的对象,其工作方式如下:

{ 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 everythingis an Object in Javascript, please put that aside for this argument. ;)

*从技术上讲,Javascript 中的一切都是对象,请将此放在一边。;)

回答by Matthew Flaschen

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>

回答by Ajay Malhotra

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

回答by Shimmy Weitzhandler

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

我有一段时间没有使用 jquery,但您可能正在寻找这个:

jQuery.makeArray(obj)

jQuery.makeArray(obj)

回答by Srikar Doddi

Here is an example that I used.

这是我使用的一个例子。

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

回答by mkoryak

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

您可以创建一个值列表,或创建一个键列表,这些都与 jquery 没有任何关系,这是纯 javascript