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

183
Views
Vue JS: filtro en API

a continuación en mi código, almacené el usuario en API JSON y quería obtener usuarios con role : Admin y su nombre de username solo, pero no funcionó. a continuación está mi respuesta y código de cuerpo JSON

 "response": [ { "profile_picture": "", "verified_by_email": false, "_id": "61c3a765c5f55200049de490", "username": "user2", "email": "user1@yahoo.com", "password": "$2a$10$pMOyjwVf1RkaIeVdNKWdaucVRybsDEEN3fjxLZOctJyL42HqrlRgC", "role": "User", "phone_number": "12365478", "createdAt": "2021-12-22T22:32:05.034Z", "updatedAt": "2021-12-22T22:32:05.034Z", "__v": 0 }, { "profile_picture": "", "verified_by_email": false, "_id": "61c325ea02526b000424929f", "username": "nina", "email": "nin@yahoo.com", "password": "$2a$10$steP5Aq9mcTIA8XLjm9Y5ONoWQUMEWLD7TJPSHzTksqbOTpv9MbD.", "role": "Admin", "phone_number": "123456", "createdAt": "2021-12-22T13:19:38.240Z", "updatedAt": "2021-12-22T13:19:38.240Z", "__v": 0 } ] }

 computed:{ getAdmin(){ // i stored my API respone in array Users this.Users.filter(users => { return users.role; } }

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Debe regresar de getAdmin y también proporcionar una comparación en el filtro para probar el rol de administrador

 computed:{ getAdmin(){ return this.Users.filter(user => user.role === 'Admin') } }

Editado en base a la pregunta modificada:

 computed:{ adminNames(){ return this.Users.filter(user => user.role === 'Admin').map(user => user.username) } }

Editado de nuevo basado en los comentarios:

Si solo espera que haya un usuario administrador en un momento dado, puede hacer esto:

 computed:{ adminName(){ const admin = this.Users.find(user => user.role === 'Admin') return admin && admin.username // or using optional chaining // return this.Users.find(user => user.role === 'Admin')?.username } }
about 4 years ago · Juan Pablo Isaza Report

0

Combine un filtro y un mapa para obtener el resultado deseado.

 var users = { "response": [ { "_id": "61c3a765de490", "username": "user2", "email": "user1@yahoo.com", "password": "$2a$10$pMOyjwVf1RkaIeVdNKWdaucVRybsDEEN3fjxLZOctJyL42HqrlRgC", "role": "User", "phone_number": "12365478", }, { "_id": "61c325e424929f", "username": "nina", "email": "nina@yahoo.com", "password": "$2a$10$steP5Aq9mcTIA8XLjm9Y5ONoWQUMEWLD7TJPSHzTksqbOTpv9MbD.", "role": "Admin", "phone_number": "123456", } ] } // Get all admins var admins = users['response'].filter((user) => user.role === 'Admin').map((user) => user.username) console.log(admins)

about 4 years ago · Juan Pablo Isaza Report
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!