Building a group of datasets into an Obj

OK, so I am building an Obj, containing several groups of data, (name,address,contact etc for each type of individual person( purchaser, seller, agent).

Within the loop, which generates the data, I set the key and value pair. Unfortunately, only the last iteration is stored as it should be with the rest missing.

I expect the dataset is being overwritten with each loop but I can’t seem to find how to add a new set to the existing Obj. I was trying to put each address/contact value into its own k => v pair but, to remove the issue more easily, I strung the data into one value, which I will change back once I get it to work.

Here’s my code.
I’m building an object of data for several ‘types’. Think purchaser, seller, agent. The only thing seemingly working wrongly is that the final dataset is overwritten with each new loop such that only the last iteration is stored.

Any pointers or assistance would be very welcome.

var partyType=''; // That var may be 'purchaser', 'seller', 'agent'

The party type string is built like this.

$party_type_data = "$persons_id:$title:$first_name:$last_name:$full_name_data:$address_name:$address_1:$address_2:$address_town:$address_county:$address_post_code:$contact_number_1:$contact_number_2:$contact_email"

Bringing that var into JS

var partyTypeData = "$party_type_data";

Build or add to the final Obj

partyTypesAddressData = {
        [partyType] : { partyTypeData }        
    }

This may help explain better

partyTypesAddressData{
    'purchaser' : { partyTypeData},
    'seller' : { partyTypeData}
    

I began building it like this but made the single string to try to simplify the working out of the issue.

partyTypesAddressData = {
      [partyType]: {
          partyId: partyId,
          partyTitle: partyTitle,
          partyFirstName: partyFirstName,
          partyLastName: partyLastName,
          partyFullName: partyFullName,
          partyAddressName: partyAddressName,
          partyAddress1: partyAddress1,
          partyAddress2: partyAddress2,
          partyAddressTown: partyAddressTown,
          partyAddressCounty: partyAddressCounty,
          partyAddressPostCode: partyAddressPostCode,
          partyContactNumber1: partyContactNumber1,
          partyContactNumber2: partyContactNumber2,
          partyContactEmail: partyContactEmail            
          
      }
      
 }

Store it in sessionStorage

    sessionStorage.setItem( 'partiesAddressData', 
    JSON.stringify(partyTypesAddressData) );  
//Outside Loop
partyTypesAddressData = {}
...
//Inside loop
partyTypesAddressData[partyType] = //However you're gonna store it.

Blimey, M. Thank you. I don’t know if I’d have thought of that, yet it seems so simple now. :blush:

1 Like

We all have those moments when it just takes a pair of fresh eyes :slight_smile:

1 Like