how can i change the if statement in a function with a ternary operator?

The code is very inefficient because you find the thing, and then you turn around and find the thing again. So you end up looping multiple times.

To make it more readable, I broke it up into parts. It also loops one time over the object to locate the items with children and the items without children. There is a ternary operator there to handle with and without.

The code then determines if it is the child or not and grabs the object.

// Grab the package
var selectedPackageProducts = => p.isSelected).dealProducts;

// check to see if the children has the product type or if the parent does (if no children)
const selectedProduct = selectedPackageProducts.find(dp => 
    dp.children.length > 0 ? 
    dp.children[0].product.productTypeCode === productType :
    dp.product.productTypeCode === productType)

// If we have children use it, else reference the parent
const productObj = selectedProduct && selectedProduct.children.length ? 
    selectedProduct.children[0] :

// get the product name 
const productName = productObj &&

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top