Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 736189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:34:35+00:00 2026-05-14T07:34:35+00:00

I am required (please therefore no nit-picking the requirement, I’ve already nit-picked it, and

  • 0

I am required (please therefore no nit-picking the requirement, I’ve already nit-picked it, and this is the req) to convert certain form fields that have “object nesting” embedded in the field names, back to the object(s) themselves. Below are some typical form field names:

  • phones_0_patientPhoneTypeId
  • phones_0_phone
  • phones_1_patientPhoneTypeId
  • phones_1_phone

The form fields above were derived from an object such as the one toward the bottom (see “Data”), and that is the format of the object I need to reassemble. It can be assumed that any form field with a name that contains the underscore _ character needs to undergo this conversion. Also that the segment of the form field between underscores, if numeric, signifies a Javascript array, otherwise an object.

I found it easy to devise a (somewhat naive) implementation for the “flattening” of the original object for use by the form, but am struggling going in the other direction; below the object/data below I’m pasting my current attempt. One problem (perhaps the only one?) with it is that it does not currently properly account for array indexes, but this might be tricky because the object will subsequently be encoded as JSON, which will not account for sparse arrays. So if “phones_1” exists, but “phones_0” does not, I would nevertheless like to ensure that a slot exists for phones[0] even if that value is null.

Data:

var obj = {
  phones: [{
      "patientPhoneTypeId": 4,
      "phone": "8005551212"
    },
    {
      "patientPhoneTypeId": 2,
      "phone": "8885551212"
    }
  ]
};

Code to date:

var unflattened = {};

for (var prop in values) {
  if (prop.indexOf('_') > -1) {
    var lastUnderbarPos = prop.lastIndexOf('_');

    var nestedProp = prop.substr(lastUnderbarPos + 1);
    var nesting = prop.substr(0, lastUnderbarPos).split("_");

    var nestedRef, isArray, isObject;

    for (var i = 0, n = nesting.length; i < n; i++) {
      if (i === 0) {
        nestedRef = unflattened;
      }

      if (i < (n - 1)) { // not last
        if (/^\d+$/.test(nesting[i + 1])) {
          isArray = true;
          isObject = false;
        } else {
          isArray = true;
          isObject = false;
        }

        var currProp = nesting[i];

        if (!nestedRef[currProp]) {
          if (isArray) {
            nestedRef[currProp] = [];
          } else if (isObject) {
            nestedRef[currProp] = {};
          }
        }

        nestedRef = nestedRef[currProp];
      } else {
        nestedRef[nestedProp] = values[prop];
      }
    }
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:34:35+00:00Added an answer on May 14, 2026 at 7:34 am

    Thanks for the other answers. Here’s how I tweaked the code I already had, I knew I was close.

    var unflattened = {};
    
    for (var prop in values) {
        if (prop.indexOf('_') > -1) {
            var nesting = prop.split("_");
            var nestedProp = nesting.pop();
    
            if (!nesting.length || !nestedProp) {
                console.log("form field name: '" + origProp + "' cannot be converted to 'nested' object");
                continue;
            }
    
            var nestedRef, currProp, isArray, isObject;
    
            for (var i=0, n=nesting.length; i<n; i++) {                 
                if (i === n-1) { //last
                    isArray = /^\d+$/.test(nestedProp);
                }
                else {
                    isArray = /^\d+$/.test(nesting[i+1]);
                }
    
                isObject = !isArray;
                currProp = nesting[i];
    
                if (i===0) {
                    nestedRef = unflattened;
                }
    
                if (!nestedRef[currProp]) {
                    if (isArray) {
                        nestedRef[currProp] = [];
                    }
                    else {
                        nestedRef[currProp] = {};
                    }
                }
    
                nestedRef = nestedRef[currProp];
    
                if (i === n-1) { //last
                    nestedRef[nestedProp] = values[prop];
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get this error when trying ruby script/console Rails requires RubyGems >= . Please
There are some other variations of this question here at SO, but please read
I'm wondering if this code ... StringBuilder sb = new StringBuilder(Please read the following
I am using timepicker from this website In my page there are multiple fields
To keep things simple, please allow the assumption that some code requires the use
How do you make a Installshield Express installation require a restart after installation? Please
Required is the true language version of the OS, not the user interface language
required( dependency-expression ) They give an example of required: #other:checked but I want my
If so what are required configuration elements to enable UTF-8 for tiles? I'm finding
Since the only operations required for a container to be used in a stack

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.