簡體   English   中英

如何將此 json object 返回到字符串中

[英]How can I return this json object into a string

我有這個 JSON object

  "name": "Luke Skywalker",
  "height": "172",
  "mass": "77",
  "hair_color": "blond",
  "skin_color": "fair",
  "eye_color": "blue",
  "birth_year": "19BBY",
  "homeworld": "Tatooine",
  "films": [
    "A New Hope",
    "The Empire Strikes Back",
    "Return of the Jedi",
    "Revenge of the Sith",
    "The Force Awakens"
  ],
}

我需要返回一個格式如下的字符串: {name}, {height}cm, {mass}kg. Featured in {film count} films. {name}, {height}cm, {mass}kg. Featured in {film count} films. 這是我到目前為止所擁有的,它給了我一個錯誤

    return `${character.name}, ${character.height} cm, ${character.mass} kg. Featured in ${character.films.length} films.`;}

[錯誤][1]: https://i.stack.imgur.com/FAciA.png

您忘記了將反引號 '``' 標記為Template-String

 const character = { "name": "Luke Skywalker", "height": "172", "mass": "77", "hair_color": "blond", "skin_color": "fair", "eye_color": "blue", "birth_year": "19BBY", "homeworld": "Tatooine", "films": [ "A New Hope", "The Empire Strikes Back", "Return of the Jedi", "Revenge of the Sith", "The Force Awakens" ] }; function getSummary(character) { return `${character.height} cm, ${character.mass} kg. Featured in ${character.films.length} films.`; } console.log(getSummary(character));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM