Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

153
Vistas
Return array and function that returns another array together JS

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?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You need to

  • actually return something from your function. If you don't return the return values of somevalues.map(...) and othervalues.map(...) then your function will return undefined.
  • call the function to get its return value
  • spread the return value into the result array, just like you do with the static array.

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()];
//                   ^^^             ^^
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

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" ]
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda