【发布时间】:2020-03-16 00:14:42
【问题描述】:
这是一个静态 json 数据的例子。
我无法从数组中渲染“promoimage”。
我不是 100% 确定如何解决这个问题。我在玩arround,想检查'promoimage'是否存在,但没有返回任何东西? 任何建议如何实现这一目标?
[
{
"title": "some title",
"promoimage": "image.jpg",
"url": "#"
},
{
"title": "some title",
"image": "example.jpg",
"url": "#"
},
{
"title": "some other title",
"promoimage": "image.jpg",
"url": "#"
},
{
"title": "title",
"image": "example.jpg",
"url": "#"
},
]
我的 React 组件:
import products from '../product-data.json';
...
export const CustomSlider = () => {
// Here I'm using react-slick
const productList = products.map((product, i) => {
const uniqueItems = [];
if (uniqueItems.indexOf(product.imageone) === -1) {
uniqueItems.push(product.imageone);
}
/* This works
if (product.hasOwnProperty('promoimage')) {
return true
}
*/
return <Product key={i} {...product} />;
}
);
return (
<Slider>
{productList}
</Slider>
)
}
【问题讨论】:
标签: arrays json reactjs object