我目前正在尝试返回一个带有一些值的array
和一个返回另一个array
的function
。如何做到我的退货基本上是2 arrays
而不是1 array
和1 function
示例
const array1 = [a, b, c]
const function = () => {
if(something) {
somevalues.map(e => {
return (
<div>{e}<div>
)
})
} else {
othervalues.map(f => {
return (
<div>{f}<div>
)
})
}
}
return [...array1, function] ??
示例中的函数显然返回了函数,而不是它自己的返回,我该如何解决这个问题?
您需要
- 实际上从函数中返回了一些内容。如果不返回
somevalues.map(...)
和othervalues.map(...)
的返回值,则函数将返回undefined
- 调用函数以获取其返回值
- 将返回值扩展到结果数组中,就像处理静态数组一样
示例:
const array1 = [a, b, c]
const outerFunction = () => {
const innerFunction = () => {
if(something) {
return somevalues.map(e => (<div>{e}<div>));
// ^^^^^^
} else {
return othervalues.map(f => (<div>{f}<div>));
// ^^^^^^
}
}
return [...array1, ...innerFunction()];
// ^^^ ^^
}
const array1 = ['a', 'b', 'c'];
const something = true;
const func = () => {
if(something) {
return 'e';
} else {
return 'f';
}
};
console.log([...array1, func()]); //[ "a", "b", "c", "e" ]