-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" }],
    [],
    []
  ]
}
1
  • Why did you choose .map()? Did you have a look at the other methods of an Array? Commented Aug 20, 2021 at 11:31

2 Answers 2

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))

Sign up to request clarification or add additional context in comments.

6 Comments

If a selectedProduct array has elements with and without ids they will still be included.
can you explain how selectedProduct.length works? @norbert.mate
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 I see what you mean and it makes sense but Emilis asked for: "remove array element if there is no selectedProducts.id"
@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.
|
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;
  }),
};

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.