0

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"],
    },
    {...},
    ...
]

3
  • how can anyone reproduce the issue you're having if you're not going to include example arrays? stackoverflow.com/help/minimal-reproducible-example Commented Apr 6, 2020 at 22:07
  • Try using incldues() in return productLib.filter(product => { return product.ID === this.productID })} like this return this.productID.includes(product.ID); Commented Apr 6, 2020 at 22:09
  • 1
    @MonteCristo you're right, I will make sure to make my issue more reproducible next time. Commented Apr 6, 2020 at 22:37

3 Answers 3

2

You can use the includes() function to check if an array contains a certain element.


Array.prototype.includes():

The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

More here.


With that said you can rewrite this:

return productLib.filter(product => {
    return product.ID === this.productID
})}

to this:

return productLib.filter(product => {
    return this.productID.includes(product.ID);
})}
Sign up to request clarification or add additional context in comments.

Comments

0

just a shot in the dark but shouldnt filterProductById look like the following:


    filterProductById() {
        // iterate through the list of similar ids
        return this.productID.map(ID => {
            // find the corresponding item in the lib
            console.log('searching for an item with ID', ID)
            return productLib.find(product => {
                console.log('found product with ID', ID, product)
                return product.ID === ID
            })
        })
        .filter(Boolean) // filter out undefined items (you can also use reduce to combine all of this)
    }

Comments

0

Replacing return product.ID === this.productID with return this.productID.includes(product.ID) solved my issue.

Here is the filterProductById method with the corrections:

filterProductById() {
        return productLib.filter(product => {
            return this.productID.includes(product.ID)
})}

-Thanks to @Ivan86

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.