将多个参数传递给Javascript函数

问题描述 投票:0回答:1

我具有如下功能:

//Calling the Function with one Parameter
responses(baseURL);

//Function Definition
function responses(baseURL) {
    $.ajax({
        url: baseURL,
        type: "get",
        cache: false,
        headers: {
            'Content-Type': 'application/json'
        },
        success: function (data) {
            console.log(data.features.length);
            for (var i = 0; i < data.features.length; i++) {
                if (taxArrayT1.indexOf(data.features[i].properties.taxon_id) == -1) {
                    taxArrayT1.push(data.features[i].properties.taxon_id);
                }
            }
            console.log("In the Invertebrate Animals Section 1");
            console.log(taxArrayT1.length);
        }
    })
}

现在,我倾向于重复我自己,因为当我使用相同功能的不同服务时。我知道如何将基本URL作为参数传递。在此示例中,还有一个数组taxArrayT1。每当使用不同的输入(例如taxArrayT2)时,此数组都会更改。如果您对如何完成工作有任何建议,那就太好了。这将有很大的帮助。

javascript html arrays ajax
1个回答
0
投票

如果我正确理解了您要执行的操作,则可以将数组添加为第二个参数。像这样:

function responses(baseURL, taxArray) {
    $.ajax({
        url: baseURL,
        type: "get",
        cache: false,
        headers: {
            'Content-Type': 'application/json'
        },
        success: function (data) {
            console.log(data.features.length);
            for (var i = 0; i < data.features.length; i++) {
                if (taxArray.indexOf(data.features[i].properties.taxon_id) == -1) {
                    taxArray.push(data.features[i].properties.taxon_id);
                }
            }
            console.log("In the Invertebrate Animals Section 1");
            console.log(taxArray.length);
        }
    })
}

并且服务呼叫将如下所示:

responses(url1, taxArrayT1);
responses(url2, taxArrayT1);
© www.soinside.com 2019 - 2024. All rights reserved.