如何将数组作为参数传递给javascript中的函数? [英] How to pass an array as argument to a function in javascript?

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

问题描述

我正在尝试制作辅助函数以使用 Google 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.

当我开始寻找解决方案时,我遇到了 此页面.在那之后,我最近遇到了这个线程这也引用了相同的链接,他们提供的格式对我没有好处.

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

我是这样称呼它的:

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

其中 desc(visits)source 都是字符串,所以我假设我正在发送一个字符串数组.奇怪的是,apply() 调用 buildString 函数中的 thisnull 都给我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天全站免登陆