In python, to return multiple variables, I can do --
在python中,要返回多个变量,我可以做 -
def function_one(i):
return int(i), int(i) * 2
value, duble_value = function_one(1)
How would I achieve this same result using javascript if functions may only return a single return value? (I assume using an array?)
如果函数只返回一个返回值,我将如何使用javascript实现相同的结果? (我假设使用数组?)
2 个解决方案
#1
33
You need to either use an array or an object.
您需要使用数组或对象。
For example:
例如:
function test() {
return {foo: "bar", baz: "bof"};
}
function test2() {
return ["bar", "bof"];
}
var data = test();
foo = data.foo;
baz = data.baz;
data = test2();
foo = data[0];
baz = data[1];
#2
-1
function foo(){
return ["something","something else","something more","something further"];
}
let [a,b,c,d] = foo();
#1
33
You need to either use an array or an object.
您需要使用数组或对象。
For example:
例如:
function test() {
return {foo: "bar", baz: "bof"};
}
function test2() {
return ["bar", "bof"];
}
var data = test();
foo = data.foo;
baz = data.baz;
data = test2();
foo = data[0];
baz = data[1];
#2
-1
function foo(){
return ["something","something else","something more","something further"];
}
let [a,b,c,d] = foo();