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

171
Vistas
How to push an array into the object in JavaScript?

I created an array and I have to ask the user for new data, and then I have to add this new information in to my original array, but I'm having problems when I trying to add the new songs written by the user.

How can i add the new songs (songName and duration) to my original array?

this is my code:

    let discos = [];
    
    let disco1 = {
        discoName: 'Disco Name ',
        band: 'Catupecumachu',
        code: 1,
        songs: [
            {
                'songName': 'song1',
                'duration': 200,
            },
            {
                'songName': 'song2',
                'duration': 180,
            },
            {
                'songName': 'song3',
                'duration': 90,
            },
        ],
    };
    let disco2 = {
        discoName: 'Disco Name',
        band: 'U2',
        code: 2,
        songs: [
            {
                'songName': 'song1',
                'duration': 200,
            },
            {
                'songName': 'song2',
                'duration': 180,
            },
            {
                'songName': 'song3',
                'duration': 90,
            },
        ],
    };
    let discoUser = {
        discoName: '',
        band: '',
        code: '',
        songs: [
            {
                'songName': '',
                'duration': '',
            },
        ],
    };

discos.push(disco1, disco2, discoUser);

  do{        
     discoUser['discoName'] = prompt('discoName');
     discoUser['band'] = prompt('band');
     discoUser['code'] = prompt('code');
  }while(confirm('Continue?'));
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Following your code example, you could try this after the last prompt line you have :

const songName = prompt('song name');
const duration = prompt('duration');
discoUser.songs.push({songName,duration});

UPDATE 1 :

Just updating to show this tested on the developer tools console : enter image description here

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