0

I try to merge two arrays:

productsAll = parserArrayFull.map((item) => {

        return {
            ...item,
            ...firestoreArrayFull.find((y) => y.productSKU === item.SELLER_SKU),
        };
    });

Everything works OK if in "firestoreArrayFull" there is "productSKU" with the same value like item.SELLER_SKU. But if not there is error "Cannot read property 'productSKU' of undefined" How to make condition for this situation? I would like to keep "item" only in the case of error. regards

1 Answer 1

2

Looks like you have some null values in firestoreArrayFull array.

Try put y && before you make a comparison to make sure y isn't null or undefined.

productsAll = parserArrayFull.map((item) => {
    return {
        ...item,
        ...firestoreArrayFull.find((y) => y && y.productSKU === item.SELLER_SKU),
    };
});

Update

  • If not found, adds productSKU property equals to item.SELLER_SKU.
productsAll = parserArrayFull.map((item) => {
    return {
        ...item,
        ...(firestoreArrayFull.find((y) => y && y.productSKU === item.SELLER_SKU) || { productSKU: item.SELLER_SKU }),
    };
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. It excluded without error now. But i need to add key productSKU which is the same like item.SELLER_SKU to returned array if not found.Regards

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.