简体   繁体   English

如何在FUNCTION中返回多变数组的输出

[英]how to return to be output for mutlidimention array in FUNCTION

can multidimensions array return in function to display the all value in the 2dimentions array ?? 多维数组可以返回功能以显示2dimentions数组中的所有值吗?

 function display(arr) { } const result = display([ ["str", 1], ["str2", 2], ["str3", 3] ]) const result2 = display([ ["str", 1], ["str2", 2], ["str3", 3], ["str4", 4] ]) const result3 = display([ ["str", 1], ["str2", 2] ]) console.log(result) console.log(result2) console.log(result3) 

i know console.log might work, how about return it in function ? 我知道console.log可能有用,如何在函数中返回它?

the output i want it // str = 1, str2 = 2, str3 = 3 // str = 1, str2 = 2, str3 = 3 , str4 = 4 // str = 1, str2 = 2 我想要的输出// str = 1, str2 = 2, str3 = 3 // str = 1, str2 = 2, str3 = 3 , str4 = 4 // str = 1, str2 = 2

This work for your code : 这适用于您的代码:

function display(arr) {
    return arr.map((key) => `${key[0]} = ${key[1]}`)
}

const result = display([
["str", 1],
["str2", 2],
["str3", 3]
]).join(', ')

// result = "str = 1, str2 = 2, str3 = 3"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM