簡體   English   中英

如何在字符串中使用forEach添加更多文本?

[英]How to add more text with forEach inside a string?

所以,我不知道這在JS中是否可行,我想做的是這樣的:

我有N個對象,並想用它們制定XML

import config from './config'; //that actually doesent matter
//just show it here because the xml that I return have a LOT of data
//from this config

buildXML(objects) => {
 return `
   <?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
   <my-xml>
     <id>${config.id}</id>
     ${objects.forEach(object) => {
       return '<object>' + object + '</object>'
     }}
   </my-xml>
 `
}

我想要給我類似的結果

  <?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
   <my-xml>
     <id>42</id>
     <object> OBJECT </object>
     <object> OBJECT </object>
   </my-xml>

有辦法嗎? 歡迎任何幫助:)謝謝!

例如:

return `
   <?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
   <my-xml>
     <id>${config.id}</id>
     ${objects.map(object => {
       return '<object>' + object + '</object>'
     }).join('')}
   </my-xml>`;

甚至更短:

return `
   <?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
   <my-xml>
     <id>${config.id}</id>
     ${objects.map(object => `<object>${object}</object>`).join('')}
   </my-xml>`;

暫無
暫無

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

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