我想创建一个对象的画廊。
角度-打字稿
我有这个功能:
showGallery(index: number) {
let prop = {
images: [
{path: 'https://web-service/attach/file/' + this.PIC[0].guid},
{path: 'https://web-service/attach/file/' + this.PIC[1].guid},
{path: 'https://web-service/attach/file/' + this.PIC[2].guid}
],
index
};
this.gallery.load(prop);
}
而且我不知道如何在此函数中进行循环。
我有一个带有GUID图片的PIC阵列。PIC是通过对数据库的API调用创建的,当然,图片的数量总是不同的。
有人可以解释一下我,为什么我不能这样做:
showGallery(index: number) {
let prop = {
images: [
for(var PICs of PIC){
{path: 'https://web-service/attach/file/' + PICs.guid}
}
],
index
};
this.gallery.load(prop);
}
这与范围界定有什么共同点吗?
如果您需要遍历数组的每个元素,map
则非常有用。它返回一个新的对象数组,该数组由传递给它的函数的返回值指定。在您的代码中,它看起来像这样:
showGallery(index: number) {
let prop = {
images: this.PIC.map(p => { path: 'https://web-service/attach/file/' + p.guid }),
index
};
this.gallery.load(prop);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句