Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.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
Javascript 从一系列图像URL计算平均尺寸';s_Javascript_Promise - Fatal编程技术网

Javascript 从一系列图像URL计算平均尺寸';s

Javascript 从一系列图像URL计算平均尺寸';s,javascript,promise,Javascript,Promise,我尝试了很多不同的方法,但仍然不起作用。有人能帮忙吗?我尝试了map()、reduce()。但始终为NaN。我不会试图通过您的测试,但这里有一个粗略的示例,说明您想要做什么: 创建一个函数loadImage,该函数“提示”图像的加载。如果图像出错resolve(null),那么您可以在以后计算平均值时将其过滤掉 然后确保加载所有图像 然后过滤掉出现错误的图像nulls 然后计算尺寸的平均值 const loadImage=url=>{ 常量图像=新图像() image.src=url 返回

我尝试了很多不同的方法,但仍然不起作用。有人能帮忙吗?我尝试了map()、reduce()。但始终为NaN。

我不会试图通过您的测试,但这里有一个粗略的示例,说明您想要做什么:

  • 创建一个函数
    loadImage
    ,该函数“提示”图像的加载。如果图像出错
    resolve(null)
    ,那么您可以在以后计算平均值时将其过滤掉
  • 然后确保加载所有图像
  • 然后过滤掉出现错误的图像
    null
    s
  • 然后计算尺寸的平均值
const loadImage=url=>{
常量图像=新图像()
image.src=url
返回新承诺(res=>{
if(image.complete)返回res(image)
image.onload=()=>res(图像)
image.onerror=()=>res(空)
})
}
const getAverageDimsOfImageURLs=imageURLs=>{
返回Promise.all(imagerurls.map(loadImage))
。然后(图像=>{
//滤除错误图像
images=images.filter(image=>image)
返回图像。减少((acc、图像、索引)=>{
附件宽度+=图像宽度
附件高度+=图像高度
if(index==images.length-1){
附件宽度=附件宽度/图像长度
附件高度=附件高度/图像长度
}
返回acc
}, {
宽度:0,
身高:0
})
})
}
常量imageURLs=[
'https://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png',
'http://tineye.com/images/widgets/mona.jpg',
'http://nonexis2tent.com/images/widgets/mona.jpg“//错误的URL。
]
getAverageDimsOfImageURLs(imageURLs)
.then(console.log)

.catch(console.error)
hi XD。。。主题首先是问题陈述,然后是问题代码、错误和更多解释。。。分类结束检查>需要编辑。@ZF007我明白了。谢谢你。@Nicholas Kyriakides非常感谢你,尼克。我试试看。
// Returns a promise that resolves with the average dimensions of all the passed in images
// Ignores any images that fail to load

function loadImages(images) {
    // Complete the body of this function
    // so that the tests below pass.

}