javascript - 将整个数组从一个函数传递到另一个函数 JavaScript
问题描述
我知道已经有很多关于这个查询的答案,但我的问题是关于接收功能。
假设我有三个功能:
function A(a){
var j = getList(a);
j != null? process.apply(null,j): null;
}
function getList(a){
// returns an array like array[][] with no definite size
}
// I know this function should accept multiple arguments but I want the whole array to be passed
function process(j){
// I want to loop the array here but it seems like
// the argument passed is value of array[0][0]
//
}
我知道在c中,它应该是:
function process(j[][]){
而python直接传j也没问题。现在,javascript 让我想知道如何实现它。非常感激您的帮忙。
解决方案
Apply
接受参数数组,但您将单个参数作为数组传递。
有几种方法可以解决这个问题,一种方法是我只是在应用中包装j
,[j]
这样它实际上将数组作为参数数组中的第一个元素传递。
我确信有更好的方法来解释这一点,但我想不出。
function A(a){
var j = getList(a);
j != null? process.apply(null,[j]): null;
}
function getList(a){
// returns an array like array[][] with no definite size
return a;
}
// I know this function should accept multiple arguments but I want the whole array to be passed
function process(j){
console.log(j);
// I want to loop the array here but it seems like
// the argument passed is value of array[0][0]
//
}
A(["1","2"]);
推荐阅读
- swift - Swift 中的扩展
- ios - 尝试编码符合 Codable 的类的子类时的 EXC_BAD_ACCESS
- f# - 如何在 F# 中按人口加权收入?
- spring-cloud-stream - 记录源应用程序错误通道的 Spring Sleuth Span Id
- ros - 将 .xacro 加载到 .xacro 中没有可见的效果
- python - HTTP 错误:403,带有美丽汤的 Python
- amazon-web-services - 在 AWS Lambda 上运行 Jest
- scripting - 如何将列中的值从 StarBasic 返回到 Calc
- c# - 打开 XML 格式不起作用
- r - 使用 R 中的列表创建数据框名称