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

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

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:

initializeSlider($(".slider-1"), ["data:xxxxxxxxxxxxx", "data:xxxxxxxxxxxxx", "data:xxxxxxxxxxxxx"]);
<% 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('base64')}` + '"';
      imageSrcs.push(imageSrc);
  } %>
  initializeSlider($(".slider-<%= i+1 %>"), [<%= imageSrcs %>]);

<% } %>

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


<details>
<summary>英文:</summary>

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?

The goal is something like this:

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

<% } %>

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


  [1]: https://i.stack.imgur.com/nfiEV.png

</details>


# 答案1
**得分**: 1

`&lt;%= imageSrcs %&gt;` 旨在将字符串插入到HTML中,而不是插入到嵌入在HTML中的JavaScript中的数组。

以安全的方式处理这个问题的方法是:

1. 将数据结构转换为JSON(即,将其变成一个字符串,而不是一个字符串的数组)。
2. 将其嵌入到一个data-*属性中(即,嵌入到HTML中,而不是嵌入在HTML中的JS)。
3. 在客户端上对其进行循环。

大致如下:

```javascript
&lt;%
const postUrls = posts.map(post =&gt; 
  post.images.map(image =&gt; 
    `data:image/${image.contentType}};base64,${image.data.toString('base64')}`;
  )
);
%&gt;
&lt;script data-postUrls=&quot;&lt;%= JSON.stringify(postUrls) %&gt;&quot;&gt;
    const postUrls = JSON.parse(document.currentScript.dataset.postUrls);
    postUrls.forEach((post, index) =&gt; {
      initializeSlider(`.slider-${index + 1}`, post);
    })
&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:

&lt;%
const postUrls = posts.map(post =&gt; 
  post.images.map(image =&gt; 
    `data:image/${image.contentType}};base64,${image.data.toString(&#39;base64&#39;)}`;
  )
);
%&gt;
&lt;script data-postUrls=&quot;&lt;%= JSON.stringify(postUrls) %&gt;&quot;&gt;
    const postUrls = JSON.parse(document.currentScript.dataset.postUrls);
    postUrls.forEach((post, index) =&gt; {
      initializeSlider(`.slider-${index + 1}`, post);
    })
&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 %>"周围添加引号,如下所示:

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

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

&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:

确定