developer tip

jquery에서 배열을 어떻게 생성합니까?

optionbox 2020. 11. 10. 08:02
반응형

jquery에서 배열을 어떻게 생성합니까?


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

jQuery에서 배열을 만들고 대신 해당 배열을 사용하는 방법 { pageNo: $(this).text(), sortBy: $("#sortBy").val()}


몇 가지 생각 :

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

마무리하려면 이걸 원하세요?

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

$("#results").load("jquery-routing.php", data);

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

다른 언어는이 두 가지를 다른 것으로 간주하며 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:

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


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>

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

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

jQuery.makeArray(obj)


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


Here is an example that I used.

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

참고URL : https://stackoverflow.com/questions/2650277/how-do-i-create-an-array-in-jquery

반응형