0
var dynastyReign = [ 
{"San Dynasty": "MXLI"},
{"Viloria Dynasty": "MCCCIIII"},
{"Tan Dynasty": "MCCCXCVIII"},  
{"Bon Dynasty": "MCDXLV"},   
{"Maiko Dynasty": "MDCLXIV"}, 
{"Paul Dynasty": "MCMXLIX"},
{"Andre Dynasty": "MMMXICX"}
];

document.write(dynastyReign.toString());

I tried to output this array with two values on each one using toString, but the output only gives me [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object].

How do I stop the object word from showing, and if possible i would also like only the roman numeral part shown, removing the "Dynasty".

Nathaniel Flick
  • 2,902
  • 2
  • 22
  • 31
Button Press
  • 623
  • 6
  • 26
  • Write a loop that prints each object the way you want it. – Barmar Mar 07 '20 at 17:50
  • 1
    The easiest method is likely to use “JSON.stringify”. The other operation can be done with a transform of the original data. – user2864740 Mar 07 '20 at 17:50
  • This question has been answered here: https://stackoverflow.com/questions/4750225/what-does-object-object-mean. You need to convert the object to a string. – Nathaniel Flick Mar 07 '20 at 17:50

3 Answers3

1

Try this:

var dynastyReign = [ 
  {"San Dynasty": "MXLI"},
  {"Viloria Dynasty": "MCCCIIII"},
  {"Tan Dynasty": "MCCCXCVIII"},  
  {"Bon Dynasty": "MCDXLV"},   
  {"Maiko Dynasty": "MDCLXIV"}, 
  {"Paul Dynasty": "MCMXLIX"},
  {"Andre Dynasty": "MMMXICX"}
];

var text = dynastyReign.map(d => Object.values(d)[0])
document.write(text);
Sushmit Sagar
  • 1,412
  • 2
  • 13
  • 27
0

var dynastyReign = [ 
{"San Dynasty": "MXLI"},
{"Viloria Dynasty": "MCCCIIII"},
{"Tan Dynasty": "MCCCXCVIII"},  
{"Bon Dynasty": "MCDXLV"},   
{"Maiko Dynasty": "MDCLXIV"}, 
{"Paul Dynasty": "MCMXLIX"},
{"Andre Dynasty": "MMMXICX"}
];

//document.write(dynastyReign.toString());

document.write(JSON.stringify(dynastyReign).replace(/Dynasty/g,''));

console.log(JSON.stringify(dynastyReign).split('Dynasty').join(''))
Jadli
  • 858
  • 1
  • 9
  • 17
0

Use dynastyReign.map(reign => Object.values(reign)[0]).join(', ');

Gabriel Vasile
  • 2,110
  • 1
  • 14
  • 26