2016-01-07 53 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 
}