简体   繁体   English

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

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

So, I dont know if this is possible in JS, what I want to do it's something like this: 所以,我不知道这在JS中是否可行,我想做的是这样的:

I have N objects, and want to formulate a XML with them 我有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>
 `
}

I wanted the result to me something like 我想要给我类似的结果

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

There's a way I can do it? 有办法吗? Any help is welcome :) Thanks! 欢迎任何帮助:)谢谢!

For instance: 例如:

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>`;

or even shorter: 甚至更短:

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