Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用map从JavaScript中的URL数组创建图像数组?_Javascript_Arrays - Fatal编程技术网

如何使用map从JavaScript中的URL数组创建图像数组?

如何使用map从JavaScript中的URL数组创建图像数组?,javascript,arrays,Javascript,Arrays,如何使用map从JavaScript中的URL数组创建图像数组?我不想渲染任何东西。只是想在JavaScript中使用它。我尝试过这个,但它在控制台中返回未定义 const urlArray = [ 'Images/black.png', 'Images/yellow.png', 'Images/pink.png', ] const imgArray = urlArray.map(url => { let img = new Image() img.src = url

如何使用map从JavaScript中的URL数组创建图像数组?我不想渲染任何东西。只是想在JavaScript中使用它。我尝试过这个,但它在控制台中返回未定义

const urlArray = [
  'Images/black.png',
  'Images/yellow.png',
  'Images/pink.png',
]

const imgArray = urlArray.map(url => {
  let img = new Image()
  img.src = url
})

console.log(imgArray)

您需要在每次迭代中返回
img

consturlarray=['Images/black.png'、'Images/yellow.png'、'Images/pink.png';
const imgArray=urlArray.map(url=>{
设img=newimage()
img.src=url;
return img;//添加这个
})
console.log(imgArray)