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

168
Views
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 answers
Answer question

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 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!