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

150
Vistas
returning an array in JavaScript returns [object] instead?

I'm attempting to return two arrays(multiple values) through declaring them as objects



<script>
    function funWithArrays(arr) {

        const arr1 = []; //short string
        const arr2 = []; //long string

        for (var i = 0; i < arr.length; i++) {
            if (arr[i].length <= 3) {
                arr1.push(arr[i]);
            } else {
                arr2.push(arr[i]);
            }
        }
        return {
            arr1,
            arr2
        };

    }
    document.getElementById("results").innerHTML = funWithArrays(["we", "cream", "to", "this", "two"]);

</script>

This is my attempt to do so, however, when I go to run it the output is as follows:

[object Object]

I'm wondering why the actual arrays aren't being returned?

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

0

You are returning incorrectly.

replace return {arr1, arr2} with return [...arr1, ...arr2] to return a single array.

Wrapping arrays in curly brackets( { } ) convert them into an object. Since you need to return an array, wrap your arrays in square brackets( [ ] ) instead.

documentation for Spread Operator

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