How to pass array to a function as its elements in string forms (Uncaught SyntaxError: Unexpected token '&')

huangapple go评论103阅读模式
英文:

How to pass array to a function as its elements in string forms (Uncaught SyntaxError: Unexpected token '&')

问题

I'm trying to pass the imageSrcs array as its array elements with quotation marks. But it doesn't really work that way. How can I pass an array to a function as its elements in string forms?

The goal is something like this:

  1. initializeSlider($(".slider-1"), ["data:xxxxxxxxxxxxx", "data:xxxxxxxxxxxxx", "data:xxxxxxxxxxxxx"]);
  1. <% for (let i = 0; i < posts.length; i++) {
  2. let imageSrcs = [];
  3. for (let j = 0; j < posts[i].images.length; j++) {
  4. let imageSrc = '"' + `data:image/${posts[i].images[j].image.contentType};base64, ${posts[i].images[j].image.data.toString('base64')}` + '"';
  5. imageSrcs.push(imageSrc);
  6. } %>
  7. initializeSlider($(".slider-<%= i+1 %>"), [<%= imageSrcs %>]);
  8. <% } %>

[![enter image description here][1]][1]

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;m trying to pass the imageSrcs array as its array elements with quotation marks. But it doesn&#39;t really work that way. How can I pass an array to a function as its elements in string forms?
  4. The goal is something like this:
  5. `initializeSlider($(&quot;.slider-1&quot;), [&quot;data:xxxxxxxxxxxxx&quot;, &quot;data:xxxxxxxxxxxxx&quot;, &quot;data:xxxxxxxxxxxxx&quot;]);`

<% for (let i = 0; i < posts.length; i++) {
let imageSrcs = [];

for (let j = 0; j < posts[i].images.length; j++) {
let imageSrc = '"' + data:image/${posts[i].images[j].image.contentType};base64, ${posts[i].images[j].image.data.toString(&#39;base64&#39;)} + '"';
imageSrcs.push(imageSrc);
} %>
initializeSlider($(".slider-<%= i+1 %>"), <%= imageSrcs %>);

<% } %>

  1. [![enter image description here][1]][1]
  2. [1]: https://i.stack.imgur.com/nfiEV.png
  3. </details>
  4. # 答案1
  5. **得分**: 1
  6. `&lt;%= imageSrcs %&gt;` 旨在将字符串插入到HTML中,而不是插入到嵌入在HTML中的JavaScript中的数组。
  7. 以安全的方式处理这个问题的方法是:
  8. 1. 将数据结构转换为JSON(即,将其变成一个字符串,而不是一个字符串的数组)。
  9. 2. 将其嵌入到一个data-*属性中(即,嵌入到HTML中,而不是嵌入在HTML中的JS)。
  10. 3. 在客户端上对其进行循环。
  11. 大致如下:
  12. ```javascript
  13. &lt;%
  14. const postUrls = posts.map(post =&gt;
  15. post.images.map(image =&gt;
  16. `data:image/${image.contentType}};base64,${image.data.toString('base64')}`;
  17. )
  18. );
  19. %&gt;
  20. &lt;script data-postUrls=&quot;&lt;%= JSON.stringify(postUrls) %&gt;&quot;&gt;
  21. const postUrls = JSON.parse(document.currentScript.dataset.postUrls);
  22. postUrls.forEach((post, index) =&gt; {
  23. initializeSlider(`.slider-${index + 1}`, post);
  24. })
  25. &lt;/script&gt;

请注意,我没有测试过这段代码。我没有时间生成posts的值或构建一个EJS测试环境。如果我有小错误,您可能需要对其进行微调。

英文:

&lt;%= imageSrcs %&gt; is designed for inserting strings into HTML, not arrays into JavaScript embedded in HTML.

The safe way to approach this is to:

  1. Convert your data structure to JSON (i.e. so it is a string, not an array of arrays of strings)
  2. Embed it in a data-* attribute (i.e. HTML not JS embedded in HTML)
  3. Loop over it client side

Something along the lines of:

  1. &lt;%
  2. const postUrls = posts.map(post =&gt;
  3. post.images.map(image =&gt;
  4. `data:image/${image.contentType}};base64,${image.data.toString(&#39;base64&#39;)}`;
  5. )
  6. );
  7. %&gt;
  8. &lt;script data-postUrls=&quot;&lt;%= JSON.stringify(postUrls) %&gt;&quot;&gt;
  9. const postUrls = JSON.parse(document.currentScript.dataset.postUrls);
  10. postUrls.forEach((post, index) =&gt; {
  11. initializeSlider(`.slider-${index + 1}`, post);
  12. })
  13. &lt;/script&gt;

Note that I haven't tested this code. I don't have time to generate a value for posts or build an EJS test environment. You might have to tweak it if I'm made minor errors.

答案2

得分: -2

尝试在"<%= imageSrcs %>"周围添加引号,如下所示:

  1. "&lt;%= imageSrcs %&gt;"
英文:

Try putting quotes around "<%= imageSrcs %>" like so:

  1. &quot;&lt;%= imageSrcs %&gt;&quot;

huangapple
  • 本文由 发表于 2023年7月6日 21:37:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629456.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定