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

160
Vistas
Replace null values to 0 in array

I need to push 0 in users element if the array's length is less than 7.

Example: If array has 2 elements. The rest should have 0 in it's users and _id as the index number.

Please help me to acheive this

 const newUsers = [
    {
        _id: 1,
        users: 8
    },
    {
        _id: 2,
        users: 6
    },
    {
        _id: 3,
        users: 1
    },
    {
        _id: 4,
        users: 8
    },
    {
        _id: 5,
        users: 0
    },
    {
        _id: 7,
        users: 0
    }
]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

for (let i = newUsers.length; i < 7; i += 1) {
  newUsers.push({ _id: i + 1, users: 0 });
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

According to this W3Schools article, you can use map operator to change all the users property.

const newUsers = [
    {
        _id: 1,
        users: 8
    },
    {
        _id: 2,
        users: 6
    },
    {
        _id: 3,
        users: 1
    },
    {
        _id: 4,
        users: 8
    },
    {
        _id: 5,
        users: 8
    }
]

if (newUsers.length < 7) 
  newUsers.map(x => x.users = 0);
  
console.log(newUsers)

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