The Delightful World of Vue Part 2
No Data Duplication! Fancy Computed Prop
  Start your All-Access Pass to unlock this challenge
Buy Access Login

Challenge 2 / 2

In the video, we showed this computed property, which merges the data returned by our watcher with the data that is present in the cart:

completeCart() {
    if (!this.cart || !this.products) {
        return null;
    }

    const completeItems = this.cart.items.map((cartItem) => (
        {
            product: this.products.find((product) => product['@id'] === cartItem.product),
            color: cartItem.color,
            quantity: cartItem.quantity,
        }
    ));

    return {
        items: completeItems,
    };
},

Take a closer look at the beginning of this function:

if (!this.cart || !this.products) {
    return null;
}

Why are we adding this check here? And what would happen if we didn't have it in place?

Skip challenges and go to theNext Chapter

Turn Challenges Off?

All further challenges will be skipped automatically.
You can re-enable challenges at any time on this page or from your account page.