3

I am trying to pass a dynamically created object into an array in JavaScript. The object passes but when I try to retrieve the data inside the object, it is gone. The console logs round[object, object]. how can I make this work?

Here's my code:

function roundList(myRounds){

var savedRounds = [];
savedRounds.push(myRounds);
console.log("this is savedRounds " + savedRounds);
};

function round(course,tee,slope,rating,score){
this.course=course;
this.tee=tee;
this.slope=slope; 
this.rating=rating;         
this.score=score;   
}

function makeRound(){
var newCourse = document.getElementById('course').value
var newTee = document.getElementById('tee').value
var newSlope = document.getElementById('slope').value
var newRating = document.getElementById('rating').value
var newScore = document.getElementById('score').value
var newDate = document.getElementById('date').value
var newRound = new round(newCourse,newTee,newSlope,newRating,newScore);
    roundList(newRound);
}
0

2 Answers 2

2

It's not clear where you're trying to access the object, but the var in var savedRounds = []; means savedRounds is only locally scoped and only accessible inside roundList method.

If you want to access savedRounds outside the roundList method, then declare savedRounds = []; outside the roundList method or return it from the method so something else can access it.

Sign up to request clarification or add additional context in comments.

1 Comment

... and it means that when "roundList" returns, the array is gone.
1

use console.log(JSON.stringify(savedRounds)) instead

That will show you the full contents of the object.

As others have pointed out, you also are not persisting the object right now, and it will not be accessible after the function completes.

1 Comment

In Chrome, you can also use console.log("Lorem Ipsum", object) and still get a nice expandable log entry. As long as you use comas instead of concating everything into a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.