如何传递一个数组作为参数在JavaScript函数? [英] How to pass an array as argument to a function in javascript?

查看:497
本文介绍了如何传递一个数组作为参数在JavaScript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使辅助函数,利用谷歌Analytics(分析)API的,我有一个简单的问题,建筑的字符串。这种情况的出现,我要申请一个过滤器,并有可能成为过滤器n个(名义金额,不超过128无论如何)。我想写这可能需要在N串和逗号分隔之间结合他们的功能。

I'm trying to make helper functions to make use of the Google Analytics API, and I have a simple problem building strings. The scenario is, I have to apply a filter, and there may be n number of filters (a nominal amount, not more than 128 anyhow). I wanted to write a function which can take in the n strings and combine them with comma-separation in between.

我不知道,如果参数的数目可以在JavaScript变量,如果可以把数组作为在javascript中的参数(我是一个新手,以JS),但我看不出有什么区别的变量只是 VAR 并没有数据类型在任何地方JS(我来自一个C ++ / Java的背景,并发现它令人困惑,因为它是)。所以,我想传递一个数组作为参数传递给函数,这样没有了。东西我可以可以是动态的工作,由数组中的元素决定。

I don't know if the number of arguments can be variable in javascript, and if it can take arrays as arguments in javascript (I am a newbie to JS), but I see no difference as variables are simply var and there is no datatype anywhere in JS (I come from a C++/Java background and find it confusing as it is). So I tried passing an array as an argument to a function so that the no. of things I can work with can be dynamic, decided by the elements in the array.

当我开始寻找解决方案,我碰到<少时href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply\"相对=nofollow>此页面。从那以后,我最近碰到<少时href=\"http://stackoverflow.com/questions/19871576/how-to-pass-arguments-array-to-another-function-in-javascript\">this螺纹其中也提到了相同的链接,他们所提供的格式不我没有好处。

When I started searching for solutions, I came across this page. After that I recently came across this thread which also refers the same link and the format they have provided does me no good.

为了清楚起见,让我提供的函数定义我写的。

For the sake of clarity, let me provide the function definition I've written.

/**
 * Utility method to build a comma-ed string from an array of strings
 * for the multiple-condition requests to the GA API
 */
function buildString(strArray)
{
    var returnString='';
    for(var x in strArray)
        returnString+=x+',';
    return returnString = returnString.substring(0, returnString.length - 1);
}

这是我怎么称呼它:

And this is how I call it:

buildString.apply(this,[desc(visits),source])

其中,递减(访问)都是字符串,所以我认为我送的一个数组字符串。奇怪的是,这两个这个适用()打电话到buildString功能给我0,1作为返回值。

where desc(visits) and source are both strings, so I assumed I'm sending an array of strings. Strangely, both this and null in the apply() call to the buildString function give me "0,1" as the return value.

请告诉我,我要去哪里错了。上午我路过阵列中的一个错误的方式?或者是我的函数的定义错了吗?或者是有一些其他更简单的方式来实现我想要?

Please tell me where I'm going wrong. Am I passing the array in a wrong manner? Or is my function definition wrong? Or is there some other simpler way to achieve what I'm trying?

推荐答案

传递阵列功能无异于传递任何其他类型的:

Passing arrays to functions is no different from passing any other type:

var string = buildString([desc(visits), source]);

然而,你的功能是不必要的,因为使用Javascript具有串联阵列元件具有一个定界符的内置函数:

However, your function is not necessary, since Javascript has a built-in function for concatenating array elements with a delimiter:

var string = someArray.join(',');

这篇关于如何传递一个数组作为参数在JavaScript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆