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

213
Vistas
Trying to push a string into an array returned from another function

Hey I'm pretty new to coding. I've tried looking around for how to do this but am at a loss.

Below are the two functions, which are in an object.

listPeople: function () {
    let array = []
    return array;
    // returns an array of all people for whom tasks exist
},
add: function ( name, task ) {
    return this.listPeople().push( name )
},

If I run this, the add function returns 1. and will return 2 if I push two things in, and so on. What's going on here, how do i push the name variable from add into the array in listpeople?? The name variable is 'zeke' by the way.

Sorry in advance if this doesn't make sense :D

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

0

It should probably look more like this. Create an arr property, and push objects into it from add. You can then return that list from listPeople.

const obj = {
  arr: [],
  listPeople: function() {
    return this.arr;
  },
  add: function(name, task) {
    return this.arr.push({ [name]: task });
  }
}

obj.add('bob', 'Shopping');
obj.add('Betty', 'Singing');
console.log(obj.listPeople());

about 4 years ago · Juan Pablo Isaza Denunciar

0

What's happening in your code is that the add function is returning the result of the .push() method (the new length of the array).

If your intent is to actually return the array, you should modify the add method as follows:

add: function ( name, task ) {
    var people = this.listPeople();
    people.push(name);
    return people;
},

Additionally, while changing listPeople to an array property instead of a method may work better on a shallow level, I don't know what you may have planned to implement into the listPeople method. And it may only confuse/complicate things as far as answering the question you have asked.

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