title: JavaScript how to build array from nested Object using recursion without mutation
body: <pre><code>interface SubscriptionParams {
    selectedProduct?: SubscriptionSelectedProduct;
  }
  
  interface SubscriptionSelectedProduct {
    productId?: string;
    pricingId?: string;
    childProduct?: SubscriptionSelectedProduct;
  }
  
  function getChildIdRecursively(product: SubscriptionSelectedProduct, ids: string[]) {
    if (product) {
      ids.push(product.productId!);
      product.childProduct &amp;&amp; getChildIdRecursively(product.childProduct, ids);
    }
  }
  
  function subscriptionProductsIds(subscription: SubscriptionParams): string[] {
    let ids: string[] = [subscription.selectedProduct?.productId!];
    if (subscription.selectedProduct?.childProduct) {
      getChildIdRecursively(subscription.selectedProduct?.childProduct, ids);
    }
  
    return ids;
  }
  </code></pre>
  <p>How to make this recursion without mutation, now I am mutating ids array.
  I want to follow functional programming principles</p>