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:
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
'developer tip' 카테고리의 다른 글
MongoDB 셸에서 복제 세트에 어떻게 연결합니까? (0) | 2020.11.10 |
---|---|
setState는 상태를 즉시 업데이트하지 않습니다. (0) | 2020.11.10 |
내가 소유하지 않은 브랜치에 대한 병합되지 않은 풀 리퀘스트를 어떻게 가져올 수 있습니까? (0) | 2020.11.10 |
UIView를 이미지로 변환하는 방법 (0) | 2020.11.09 |
Python에서 두 개의 정렬 된 목록 결합 (0) | 2020.11.09 |