0

currently this displays an array of objects on the page . I only want it to display the name and the points with a few spaces in between . for example , Player1 25 . I don't want anything else like the []{} etc

<p id="demo"></p>

<script>

Player1 = { name: 'Player1', points: 25 };
Player2 = { name: 'Player2', points: 50 };
Player3 = { name: 'Player3', points: 14 };

players = [Player1, Player2, Player3];

document.getElementById("demo").innerHTML = JSON.stringify(players) ;

1
  • Why not just loop over the objects and access the properties you want by key? Commented Jan 23, 2019 at 4:32

2 Answers 2

3

Just loop through players and then spread the values of each player:

var Player1 = {
  name: 'Player1',
  points: 25
};
var Player2 = {
  name: 'Player2',
  points: 50
};
var Player3 = {
  name: 'Player3',
  points: 14
};

var demo = document.getElementById("demo");

var players = [Player1, Player2, Player3];

players.forEach(player => demo.innerHTML += "<br>" + Object.values(player).join(" "));
<p id="demo"></p>

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

Comments

0

You can initialize a empty string and create span tags and concatenate the results. An approach of code is below in jsfiddle.

http://jsfiddle.net/trgwq7xy/

Player1 = { name: 'Player1', points: 25 };
Player2 = { name: 'Player2', points: 50 };
Player3 = { name: 'Player3', points: 14 };

players = [Player1, Player2, Player3];

html$ = '';
for(var key in players){
 html$ += '<span>'+players[key].name+', '+ players[key].points+'</span> ';
}

document.getElementById("demo").innerHTML = html$ ;

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.