简体   繁体   English

将多个参数传递给 Javascript 函数

[英]Passing multiple parameters to a Javascript function

I have a function which is as follows:我有一个功能如下:

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

Now I tend to repeat myself because when I hit different services with the same function.现在我倾向于重复自己,因为当我使用相同的功能访问不同的服务时。 I know how to pass the base URL as a parameter.我知道如何将基本 URL 作为参数传递。 Also there is an array like in this example, taxArrayT1 .还有一个类似于本例中的数组taxArrayT1 This array changes every time a different input is used, like taxArrayT2 .每次使用不同的输入时,此数组都会更改,例如taxArrayT2 It would be great if you have suggestions on how to accomplish.如果您有关于如何完成的建议,那就太好了。 It would be of great help.会有很大帮助。

If I understand correctly what you are trying to do then you can just add the array as a second parameter.如果我正确理解您要做什么,那么您可以将数组添加为第二个参数。 Like this:像这样:

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

And the service calls will look like this:服务调用将如下所示:

responses(url1, taxArrayT1);
responses(url2, taxArrayT1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM