2016-01-07 58 views
0

我試圖將2d數組傳遞給函數。但是當我循環訪問setParams中的數組時,數組中有一些額外的值(發生在switch中的每case),我不知道它們來自哪裏。如何通過值函數傳遞二維數組

function setParams(button, params) { 


     $mydialog = $("#dialog").dialog({ 
      autoOpen: true, 
      buttons: { 
       "ok": function() { 
        $(this).dialog("close"); 
        for(var key in params) { 
         var value = params[key].toString(); 

         var arr = value.split(','); 
         alert(arr[0] + "  " + arr[1]); 

         var control = $("#dialog").find('input[name='+arr[1]+']'); 

         if (control.is(':checkbox')) 
         { 
          button.data('id')[arr[0]] = control.is(":checked"); 
         } 
         else 
         { 
          button.data('id')[arr[0]] = control.val(); 
         } 
        } 
        sendRequest(button); 
       }, 
       "cancel": function() { 
        $(this).dialog("close"); 
       } 
      } 
     }); 
    } 

菜單功能

$(function() { 
     $('#navigationMenu a').click(function(e) { 
      e.preventDefault(); 
      e.stopPropagation(); 

      var button = $(this); 
      var cl = button.attr("class"); 
      console.log(button.attr('href')); 

      switch(cl) { 
       case "input-params": 
        $('.id').show(); 
        $('.requestDate').hide(); 
        $('.startEndDate').hide(); 
        setParams(button, [["id","id"]]); 
        break; 
       case "input-params-location": 
        $('.id').show(); 
        $('.requestDate').hide(); 
        $('.startEndDate').hide(); 
        setParams(button, [["locationId","id"]]); 
        break; 
       case "input-params-openinghours": 
        $('.id').show(); 
        $('.requestDate').show(); 
        $('.startEndDate').hide(); 
        var myArray = [["locationId","id"],["requestDate","date"]]; 
        setParams(button, myArray); 
        break; 
       case "input-params-allocations": 
        $('.id').hide(); 
        $('.requestDate').hide(); 
        $('.startEndDate').show(); 
        setParams(button, [["startDate","startDate"],["endDate","endDate"],["includeGroupActivities","includeGroupActivities"]]); 
        break; 
       case "input-params-allocations-id": 
        $('.id').show(); 
        $('.requestDate').hide(); 
        $('.startEndDate').show(); 
        setParams(button, [["id","id"],["startDate","startDate"],["endDate","endDate"],["includeGroupActivities","includeGroupActivities"]]); 
        break; 
       default: 
        sendRequest(button) 
      } 

     }); 
    }); 

除我通過這些值的值是數組太

1)

function(){return v; 
} undefined 

2)

function Array() {[native code]} undefined 

3)

function (i v)Array.forEach(this 

我怎麼能傳遞沒有得到額外的值以正確的方式排列?

+0

由於它是一個二維數組,你應該能夠得到像這樣的'params [0] [0]''的值。這將獲得第一個元素的第一個值。所以當你用'params [0] [0]'得到'[['hey','test],['hey1','test1']''時,你會得到值''hey'''。 – wouter140

回答

0

從你的代碼中不清楚2d數組是什麼意思,因爲對我來說它只是看起來像一個帶有字符串值的數組。

您獲得的額外值是數組對象上的函數。當你做一個for-in循環時,你循環了對象的屬性。在數組的情況下,這既是數組的內容(屬性「0」,「1」等)和本地函數。如果您想循環使用數組的內容,則應使用常規的for循環或數組上存在的forEach函數。

如果您立即想要使用for-in循環,則可以檢查該屬性是來自原型還是存在於具有hasOwnProperty功能的實例中。

for(var key in params) { 
    if(!params.hasOwnProperty(key){ 
    return; 
    } 

//rest of the code 
}