0

When a button is clicked i want the results in the array to be listed for example: John Smith 16, Jack Snow 10 etc..

I want to use a loop however the code in my loop is incorrect at the moment as when i click the button all i get is: [object Object].

Can someone provide a possible fix?

 function begin() {
      listresults();
      ();
    }
    var results1 = {name:"John Smith", score:16};
    var results2 = {name:"Jack Sow", score:10};
    var results3 = {name:"Tessa Flip", score:15};
    var results = [results1, results2, results3];

    function listresults() {
      var text = "";
      var total = 0;
      var i;
    for (i in results) {
      text += results[i] + "<br>";
    }
    document.getElementById('message').innerHTML = text;


    }
3
  • 2
    thats because you are appending objects instead of their values try : text += results[i].name + " " + results[i].score + "<br>"; Commented May 17, 2017 at 6:30
  • apending object try this text += results[i].name + ' ' results[i].score + "<br>"; Commented May 17, 2017 at 6:31
  • Check my single line answer. I hope it will work as per your expectation Commented May 17, 2017 at 9:04

5 Answers 5

1

I would first check that the lengths of the 2 arrays are the same. Then iterate using a for loop:

final int timeLength = TIME.length;
if (timeLength != stat.size()) {
    //something may not be right
}
for (int i = 0; i < timeLength; i++) {
    System.out.println(time[i]+" "+stat.get(i));
}
Sign up to request clarification or add additional context in comments.

Comments

0

You are pushing objects results1, results2, etc in the array 'results'. So while iterating the array you should access the object properties as shown below:

function listresults() {
      var text = "";
      var total = 0;
      var i;
    for (i in results) {
      text += results[i]['name'] + ' ' + results[i]['score'] + "<br>";
    }

Comments

0

As you are appending objects instead of object values in the filed.

This is the proper way of accessing name and score from object which is returned when you are looping through your array of objects :

 function begin() {
      listresults();
      ();
    }
    var results1 = {name:"John Smith", score:16};
    var results2 = {name:"Jack Sow", score:10};
    var results3 = {name:"Tessa Flip", score:15};
    var results = [results1, results2, results3];

    function listresults() {
      var text = "";
      var total = 0;
    for (var i=0; i < results.length; i++) {
      text += results[i].name + " " + results[i].score + "<br>";
    }
    document.getElementById('message').innerHTML = text;
    }

Here is an Jsfiddle example

Comments

0

Recommend you to use Array methods(map, join) instead of pure loops

function begin() {
      listresults();
    }
    
    var results1 = {name:"John Smith", score:16};
    var results2 = {name:"Jack Sow", score:10};
    var results3 = {name:"Tessa Flip", score:15};
    var results = [results1, results2, results3];
    
    function listresults() {
      document.getElementById('message').innerHTML = 
         results.map(function(item) { 
           return item.name + ' ' + item.score; 
         }).join('<br>');
         
      document.getElementById('total').innerHTML = 
         results.map(function(item) { 
           return item.score;
         }).reduce(function(sum, score) { 
           return sum + score;
         }, 0);
    }
<button onclick="begin()">begin</button>
<br />
<div id="message"></div>
<div>total: <span id="total">0</span></div>

3 Comments

thanks! One more question, how would i be able to total all of the scores? for example, 41 would be the total in this instance.
in your for loop just do total += results[i].score; and after for loop console.log it or use it
@J.Doe added total score in example above
0

Use Array.map() and Array.join()

var results1 = {name:"John Smith", score:16};
var results2 = {name:"Jack Sow", score:10};
var results3 = {name:"Tessa Flip", score:15};
var results = [results1, results2, results3];  

var res = results.map(item => { return item.name+ " " +item.score });

console.log(res.join(", "));

Comments

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.