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 = this.deal.packages.find(p => 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.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 : selectedProduct; // get the product name const productName = productObj && productObj.product.name
CLICK HERE to find out more related problems solutions.