简体   繁体   English

将图像添加到 javascript 中的数组

[英]adding image to array in javascript

My code:我的代码:

var a = ["1","2","3"]
console.log(a)

output is [1,2,3] output 为 [1,2,3]

The expected output is:预期的 output 为:

1.jpg, 2.jpg, and 3.jpg ( all images ) 1.jpg、2.jpg 和 3.jpg(所有图像)

My code is running well in webstorm and giving the expected output.我的代码在 webstorm 中运行良好,并给出了预期的 output。 How do I add images?如何添加图像? Whenever I try to get an answer for this I'm getting redirected to a new language or framework.每当我试图得到这个答案时,我都会被重定向到一种新的语言或框架。 How do I add design to my javascript code?如何向我的 javascript 代码添加设计?

As I understood, you want to add a postfix to your array, so the code will be:据我了解,您想为数组添加一个后缀,因此代码将是:

a.map(item => `${item}.jpg`)

 const a = ["1", "2", "3"]; const imageFolderPath = "/"; const imageExtension = '.jpg'; var imagePaths = a.map((name) => imageFolderPath + name + imageExtension ); function createImages(imagePaths) { return imagePaths.map((imagePath) => { const image = document.createElement("img"); image.setAttribute("src", imagePath); return image; }); } function addImagesToDiv(divId, images) { const theDiv = document.getElementById(divId); images.forEach((image) => theDiv.appendChild(image)); } const images = createImages(imagePaths); addImagesToDiv("myDiv", images);
 <div id="myDiv"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM