最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to return multiple values ​with a function? - Stack Overflow

programmeradmin0浏览0评论

In what way could I make the function return 3 values ​​to me so that those 3 values ​​would serve as input parameters for another function (as if it were a callback --which in fact, I think it is--)?

Until now, try the following (although it did not work):

var resultado;
function num_2(repet){
	for (var i = 0; i > repet-1; i++) {
		if (i>=1) {
			return i + ", ";
		}else{
			return i;
		}
	}
}


function sumarr(a,b,c){
	if (a!="" && a!=null) {
		resultado = a+b+c;
		return "\n" + "resul: " + resultado + '\n' + "1: " +
		a + '\n' + "2: " + b + '\n' + "3: " + c + '\n' + '\n';
	}else{
		return "noting";
	}
}
console.log("\n" + "callback 2: " + sumarr(num_2(3)));

In what way could I make the function return 3 values ​​to me so that those 3 values ​​would serve as input parameters for another function (as if it were a callback --which in fact, I think it is--)?

Until now, try the following (although it did not work):

var resultado;
function num_2(repet){
	for (var i = 0; i > repet-1; i++) {
		if (i>=1) {
			return i + ", ";
		}else{
			return i;
		}
	}
}


function sumarr(a,b,c){
	if (a!="" && a!=null) {
		resultado = a+b+c;
		return "\n" + "resul: " + resultado + '\n' + "1: " +
		a + '\n' + "2: " + b + '\n' + "3: " + c + '\n' + '\n';
	}else{
		return "noting";
	}
}
console.log("\n" + "callback 2: " + sumarr(num_2(3)));

Share Improve this question asked May 23, 2019 at 2:27 Tux9000Tux9000 1551 silver badge7 bronze badges 6
  • 1 Can you please tell the expected output of your code? – Maheer Ali Commented May 23, 2019 at 2:33
  • 2 return an array and and spread it in your function params – AsukaSong Commented May 23, 2019 at 2:35
  • 1 You cannot do that in JS. You could pass the parameters as an array to help your case. – varun agarwal Commented May 23, 2019 at 2:35
  • 1 The for loop in num_2 will only ever go through one iteration, because of the returns. It looks like you meant to add i to an array or string, which you return later as a single variable. – James Commented May 23, 2019 at 2:36
  • 1 Sorry you can only return one thing from a function. But that thing can contain other things. – James Commented May 23, 2019 at 2:41
 |  Show 1 more ment

4 Answers 4

Reset to default 4

You can return an array from the first function and pass each element to another other function as independent argument using Spread Operator.

var resultado;
function num_2(repet){
  return [...Array(repet)].map((x,i) => i)
}
function sumarr(a,b,c){
	return a + b + c
}
console.log("\n" + "callback 2: " + sumarr(...num_2(3)))

If its hard for you to understand the first function so its same as

function num_2(repet){
  let res = [];
  for(let i = 0;i<repet;i++){
     res.push(i)
  }
  return res;
}

You have two options; return a JSON object, return an array.

Returning a JSON object you can name the properties then modify the second function to accept the object and access the properties;

var input = function() { 
   return { 
     a: 1, 
     b: 2 
   }; 
}
   
var output = function(obj) { 
   console.log(obj.a);
   console.log(obj.b);
}

output(input());

Returning an array and using that as the arguments to call the function with apply() or with spread syntax

var input = function() { 
   return [1, 2]; 
}
  
var output = function(a, b) { 
   console.log(a);
   console.log(b);
}

output.apply(this, input());

output(...input());

These all have the same result printing the following in the console;

1
2

with JS object :

var Vals = Return3Values();

console.log( Vals.a, Vals.b, Vals.c );

function Return3Values()
{
 return {a:5, b:'hello', c:15 }
}

If you are able to use the latest JS features, you can use Destructuring Assignment in your functions to solve this and clean up your code.

function logLotsOfThings({thing1, thing2, thing3}) {
  console.log(thing1, thing2, thing3)
}

function getLotsOfThings() {
  return {
    thing1: "I am thing 1",
    thing2: "I am thing 2",
    thing3: "I am thing 3"
  }
}

logLotsOfThings(getLotsOfThings())

发布评论

评论列表(0)

  1. 暂无评论