-2

This is my data:

{
  "productGroups": [
    {
      "selectedProducts": [
        { "productPricing": { "recurringFee": {}, "oneTimeFee": {} }, "id": "610e99f9b13b4126a9e07e36" }
      ]
    },
    {
      "selectedProducts": [
        { "productPricing": { "recurringFee": {}, "oneTimeFee": {} } }
      ]
    },
    {
      "selectedProducts": [
        { "productPricing": { "recurringFee": {}, "oneTimeFee": {} } }
      ]
    }
  ]
}

I want to remove array element if there is no selectedProducts.id

so result should be:

{
  "productGroups": [
    {
      "selectedProducts": [
        { "productPricing": { "recurringFee": {}, "oneTimeFee": {} }, "id": "610e99f9b13b4126a9e07e36" }
      ]
    }
  ]
}

This is what i tried:

const filteredData = {
  productGroups: data.productGroups.map(productGroup => {
    const selectedProduct = productGroup.selectedProducts?.filter(product => product.id);
    return selectedProduct;
  }),
};

My result is wrong and I get result with empty arrays:

{
  "productGroups": [
    [
      { "productPricing": { "recurringFee": {}, "oneTimeFee": {} }, "id": "610e9a5eb13b4126a9e07e37" }],
    [],
    []
  ]
}
pilchard
  • 12,414
  • 5
  • 11
  • 23
Emilis
  • 152
  • 1
  • 4
  • 21
  • Why did you choose [`.map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)? Did you have a look at the other methods of an [`Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)? – Andreas Aug 20 '21 at 11:31

2 Answers2

1

let data = {
  "productGroups": [
    {
      "selectedProducts": [
        { "productPricing": { "recurringFee": {}, "oneTimeFee": {} }, "id": "610e99f9b13b4126a9e07e36" }
      ]
    },
    {
      "selectedProducts": [
        { "productPricing": { "recurringFee": {}, "oneTimeFee": {} } }
      ]
    },
    {
      "selectedProducts": [
        { "productPricing": { "recurringFee": {}, "oneTimeFee": {} } }
      ]
    }
  ]
};


const filteredData = {
  productGroups: data.productGroups.filter(productGroup => {
    const selectedProduct = productGroup.selectedProducts.filter(product => product.id);
      return selectedProduct.length;
  }),
};


console.log(JSON.stringify(filteredData))
norbert.mate
  • 302
  • 1
  • 5
  • If a `selectedProduct` array has elements with and without ids they will still be included. – pilchard Aug 20 '21 at 11:35
  • can you explain how selectedProduct.length works? @norbert.mate – Emilis Aug 20 '21 at 11:38
  • since your inner `filter` isn't actually filtering anything it would be more efficient to just use a `some()`. Some exits early, while filter by necessity iterates all elements. `productGroups: data.productGroups.filter(({ selectedProducts }) => selectedProducts.some(({ id }) => id !== undefined)),` – pilchard Aug 20 '21 at 11:41
  • @pilchard I see what you mean and it makes sense but Emilis asked for: "remove array element if there is no selectedProducts.id" – norbert.mate Aug 20 '21 at 12:26
  • @Emilis if there is no element with id the array will be empty so the length will be 0. Array filter works by checking if the returned value is true/false. In this case 0 will be evaluated as false. – norbert.mate Aug 20 '21 at 12:26
  • I understand how you are using filter() and my proposed use of `some()` achieves the same thing without having to look at every element. – pilchard Aug 20 '21 at 13:01
0

This is how I would do it:

const filteredData = {
  productGroups: data.productGroups.filter(productGroup => {
     return (productGroup.selectedProducts.filter((product) => (product.id !== undefined && product.id != '') ? true: false)).length;
  }),
};
Haider Ali
  • 1,081
  • 8
  • 23