英文:
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'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]
[1]: https://i.stack.imgur.com/nfiEV.png
</details>
# 答案1
**得分**: 1
`<%= imageSrcs %>` 旨在将字符串插入到HTML中,而不是插入到嵌入在HTML中的JavaScript中的数组。
以安全的方式处理这个问题的方法是:
1. 将数据结构转换为JSON(即,将其变成一个字符串,而不是一个字符串的数组)。
2. 将其嵌入到一个data-*属性中(即,嵌入到HTML中,而不是嵌入在HTML中的JS)。
3. 在客户端上对其进行循环。
大致如下:
```javascript
<%
const postUrls = posts.map(post =>
post.images.map(image =>
`data:image/${image.contentType}};base64,${image.data.toString('base64')}`;
)
);
%>
<script data-postUrls="<%= JSON.stringify(postUrls) %>">
const postUrls = JSON.parse(document.currentScript.dataset.postUrls);
postUrls.forEach((post, index) => {
initializeSlider(`.slider-${index + 1}`, post);
})
</script>
请注意,我没有测试过这段代码。我没有时间生成posts
的值或构建一个EJS测试环境。如果我有小错误,您可能需要对其进行微调。
英文:
<%= imageSrcs %>
is designed for inserting strings into HTML, not arrays into JavaScript embedded in HTML.
The safe way to approach this is to:
- Convert your data structure to JSON (i.e. so it is a string, not an array of arrays of strings)
- Embed it in a data-* attribute (i.e. HTML not JS embedded in HTML)
- Loop over it client side
Something along the lines of:
<%
const postUrls = posts.map(post =>
post.images.map(image =>
`data:image/${image.contentType}};base64,${image.data.toString('base64')}`;
)
);
%>
<script data-postUrls="<%= JSON.stringify(postUrls) %>">
const postUrls = JSON.parse(document.currentScript.dataset.postUrls);
postUrls.forEach((post, index) => {
initializeSlider(`.slider-${index + 1}`, post);
})
</script>
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 %>"周围添加引号,如下所示:
"<%= imageSrcs %>"
英文:
Try putting quotes around "<%= imageSrcs %>" like so:
"<%= imageSrcs %>"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论