I have working code
const products = this.state.products.map((product, i) =>
product.fields.company.fields.slug === this.props.match.params.slug ?
<Suspense key={i} fallback={<div>Loading...</div>}>
<ProductListItem id={i} key={i} product={product} />
</Suspense>
: null)
return(
<div className="partner-details" style={partnerD}>
<div className="container-xl">
<Button type="button" className="btn btn-secondary" onClick={this.props.history.goBack}>
<i className="fa fa-arrow-left"></i> Get back
</Button>
<ul>
<div className="product-item">
{products}
</div>
</ul>
</div>
</div>
)
But the problem is if product.fields.company.fields.slug (company.fields.slug) does not exist my code crashes.
How can I add extra ternary operator to check if it product.fields.company exist before execute this product.fields.company.fields.slug === this.props.match.params.slug
Thanks!
(product.fields && product.fields.company && product.fields.company.fields && product.fields.company.fields.slug) === this.props.match.params.slug ...but then you'll have another problem because you're not doing anything with the result of the ternary operation.this.state.products.filter(...).map(...)to remove all elems without a specific field.