Javascript 如何将二维数组按值传递给函数

Javascript 如何将二维数组按值传递给函数,javascript,arrays,multidimensional-array,Javascript,Arrays,Multidimensional Array,我正在尝试将2d数组传递给函数。但是,当我在setParams中循环遍历数组时,数组中有一些额外的值(在开关中的每个情况下都会发生),我不知道它们来自哪里 function setParams(button, params) { $mydialog = $("#dialog").dialog({ autoOpen: true, buttons: { "ok": function() {

我正在尝试将2d数组传递给函数。但是,当我在
setParams
中循环遍历数组时,数组中有一些额外的值(在
开关中的每个
情况下都会发生),我不知道它们来自哪里

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

        });
    });
除了我传递的值之外,这些值也在数组中

(一)

(二)

(三)


如何以正确的方式传递数组而不获取额外的值?

从您的代码中,不清楚2d数组的含义,因为在我看来,它只是具有字符串值的数组

获得的额外值是数组对象上的函数。执行for in循环时,将循环对象属性。对于数组,这既是数组的内容(属性“0”、“1”等)也是本机函数。如果要循环数组的内容,则应使用常规for循环或数组中存在的forEach函数

如果您想立即使用for in循环,可以使用
hasOwnProperty
-函数检查该属性是来自原型还是存在于实例上

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

//rest of the code
}

由于它是一个2D数组,您应该能够获得如下值
params[0][0]
。这将获得第一个元素的第一个值。因此,当您使用
参数[0][0]
得到这个
[[“嘿”,“测试],“hey1”,“test1”]
时,您将得到值
“嘿”
function Array() {[native code]}   undefined
function (i  v)Array.forEach(this
for(var key in params) {
  if(!params.hasOwnProperty(key){
    return;
  }

//rest of the code
}