I have trouble finding a way to loop through the elements of an array to filter another array of objects. I found a way to iterate through the this.productID when there's only one item, but when this.productID is an array, the elements are shown with their content tagged as undefined.
Thanks for the help!
Note: productLib is passed down as product from its parent component
export class Product extends React.Component {
constructor(props){
super(props);
this.productID = this.props.product.similarItems;
this.filterProductById = this.filterProductById.bind(this);
}
filterProductById() {
return this.productID.map(ID => {
return productLib.filter(product => {
return product.ID === ID
})
})}
/*
The code below only works when there's a single string in this.productID
return productLib.filter(product => {
return product.ID === this.productID
})}*/
render() {
return(
<div className="recommended">
{
this.filterProductById().map(product => {
return <Products product={product} key={product.ID}/>
})
}
</div>
)
}
}
Here is how the data in productLib is formated:
export const productLib = [
{
ID: "001",
...,
similarItems: ["004", "002", "001", "015"],
},
{...},
...
]
incldues()inreturn productLib.filter(product => { return product.ID === this.productID })}like thisreturn this.productID.includes(product.ID);