Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda