Skip JSON.map() for the subsequent elements

DEMO
(Please check the browser console for output)

I have a JSON customerItemResponse in a format

{
    "totalResults": someNumber,
    "results": [
        {
            "totalItem": 406,
            "customerId": "10000"
        },
        {
            "totalItem": 468,
            "customerId": "10001"
        },
        {
            "totalItem": 20,
            "customerId": "10002"
        },
...

Then I have another JSON customerInfo:

{
    "totalResults": someNumber,
    "results": [
        {
            "customerId": "10000",
            "region": "4",
            "area": "42",
        },
        {
            "customerId": "10001",
            "region": "4",
            "area": "43",
        },
        {
            "customerId": "10002",
            "region": "5",
            "area": "52",
        },

Now I have to create a JSON in a format

[
    {
        region:'4'
        regionDetails:[
                          {
                              area:'42'
                              customerDetails:[
                                              {
                                                   customerId:'10000'
                                                   totalItem:406 
                                              },
                                              {
                                                   customerId:'10005'
                                                   totalItem:301 
                                              },
                                              ]
                          },
                          {
                              area:'11'
                              customerDetails:[
                                              {
                                                   customerId:'10010'
                                                   totalItem:11 
                                              },
                                              {
                                                   customerId:'10021'
                                                   totalItem:105 
                                              },
                                              ]
                          },
                      ]
    },
    {
        region:'5'
        regionDetails:[
                          {
                              area:'52'
                              customerDetails:[
                                              {
                                                   customerId:'10002'
                                                   totalItem:52 
                                              },
                                              {
                                                   customerId:'10027'
                                                   totalItem:310 
                                              },
                                              ]
                          },
                          {
                              area:'41'
                              customerDetails:[
                                              {
                                                   customerId:'10017'
                                                   totalItem:109 
                                              },
                                              {
                                                   customerId:'10041'
                                                   totalItem:450 
                                              },
                                              ]
                          },
                      ]
    }

]

This is the logic I have written:

customerData=<CustomerDataInterface[]>[]

mapJson() {
        this.customerItemResponse.map((res, index) => {
            this.customerInfo.find((obj) => {
                if (obj.customerId == res.customerId) {
                    this.customerData.length
                        ? this.customerData.map((data, index1) => {
                              if (data.region == obj.region) {
                                  data.regionDetails.length
                                      ? data.regionDetails.map((regDetails, index2) => {
                                            if (regDetails.area == obj.area) {
                                                regDetails.dealerDetails.push({
                                                    customerId: obj.customerId,
                                                    totalItem: res.totalItem,
                                                });
                                                return;
                                            }
                                            if (index2 == data.regionDetails.length - 1) {
                                                data.regionDetails.push({ area: obj.area, dealerDetails: [] });
                                            }
                                        })
                                      : data.regionDetails.push({ area: obj.area, dealerDetails: [] });
                                  return;
                              }
                              if (index1 == this.customerData.length - 1) {
                                  this.customerData.push({ region: obj.region, regionDetails: [] });
                              }
                          })
                        : this.customerData.push({ region: obj.region, regionDetails: [] });
                }
            });
        });
        console.log(this.customerData);
    }

Now the output of the console has several region repeated. And suppose if I have 6 unique region but the this.customerData.length is 31.

I think return; is not working as expected. And is not skipping the subsequent element.

34 thoughts on “Skip JSON.map() for the subsequent elements”

Leave a Comment