【发布时间】:2017-07-14 10:39:10
【问题描述】:
示例:https://jsfiddle.net/zidski/a0ez5sy9/1/
我创建了一个名为 ProjectAPI 的 JSON 对象。
const ProjectAPI = {
projects: [
{
name: "PROJECT NAME",
thumbnail: "imageURL",
image: [
"imageURL",
"imageURL",
"imageURL"
]
},{
name: "PROJECT NAME",
thumbnail: "imageURL",
image: [
"imageURL",
"imageURL",
"imageURL"
]
}],
all: function() { return this.projects},
get: function(id) {
const isProject = p => p.number === id
return this.projects.find(isProject)
}
}
然后我使用 .map 来获取嵌套图像:
{
ProjectAPI.all().map(function(item, index) {
return <img className="img" key={index} src={item.image[index]} />
})
}
但似乎是循环遍历父数组,所以我最终得到 6 个图像而不是 3 个(在 jsfiddle 示例中边框红色)
我怎样才能只定位嵌套图像?
【问题讨论】:
-
so I end up with 6 images instead of 3你的意思是3 images instead of 6? -
是 3 张图片而不是 6 张。
标签: javascript arrays reactjs