Reputation: 545
I'm currently trying to return an array
with some values and a function
that returns another array
as well. How do I do it that my returns is basically 2 arrays
instead of 1 array
and 1 function
Example
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] ??
function in the example obviously returns function instead of its own return, how do I fix that?
Upvotes: 0
Views: 502
Reputation: 816334
You need to
somevalues.map(...)
and othervalues.map(...)
then your function will return undefined
.Example:
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()];
// ^^^ ^^
}
Upvotes: 1
Reputation: 2005
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" ]
Upvotes: 0