Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

107
Views
JavaScript devuelve una matriz diferente dentro y fuera de Observable

Tengo una función que debería devolver una matriz de Objeto con 2 propiedades:

 setusersExpiryDate() { let userList = []; let userList2 = []; for (let i = 0; i < this.usersIdsList.length; i++) { this.userService.getUserDetails(this.usersIdsList[i]).subscribe( user => { if (user.type === 'INTERNAL') { userList[i] = { user: this.usersIdsList[i], expirationDate: this.getInternalExpiryDate() } } if (user.type === 'EXTERNAL') { userList[i] = { user: this.usersIdsList[i], expirationDate: this.getExternalExpiryDate() } } }, error => console.log(error) ); userList2[i] = { user: this.usersIdsList[i], expirationDate: this.getInternalExpiryDate() } } console.log(userList); console.log(userList2); return userList; }

la primera lista de usuarios de la matriz devolverá algo como esto:

 [] 0: {user: 'USER222', expirationDate: Tue Oct 25 2022 12:32:45 GMT+0100 (Western European Summer Time)} 1: {user: 'USER321', expirationDate: Sun Oct 25 2026 12:32:45 GMT+0000 (Western European Standard Time)} 2: {user: 'USER011', expirationDate: Sun Oct 25 2026 12:32:45 GMT+0000 (Western European Standard Time)} length: 3 [[Prototype]]: Array(0)

y la segunda matriz userList2 , que está fuera de Observable, está dando el resultado correcto:

 (3) [{…}, {…}, {…}] 0: {user: 'USER222', expirationDate: Mon Oct 25 2021 12:32:45 GMT+0100 (Western European Summer Time)} 1: {user: 'USER321', expirationDate: Mon Oct 25 2021 12:32:45 GMT+0100 (Western European Summer Time)} 2: {user: 'USER011', expirationDate: Mon Oct 25 2021 12:32:45 GMT+0100 (Western European Summer Time)} length: 3 [[Prototype]]: Array(0)

Quiero saber qué estoy haciendo mal con la matriz userList, porque quiero que devuelva el mismo resultado que userList2.

userList => [] matriz vacía: Incorrecto / userList2 => [{…}, {…}, {…}] matriz con 3 objetos: correcto

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!