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 8578099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:18:35+00:00 2026-06-11T20:18:35+00:00

i have a javascript code which generates dynamic sub arrays. function createArray() { var

  • 0

i have a javascript code which generates dynamic sub arrays.

function createArray() {

    var myArr = new Object();
    myArr[0] = createSubArray('apple');
    myArr[1] = createSubArray('Mango');
    myArr[2] = createSubArray('Pineapple');
    myArr[3] = createSubArray('Grape');
    document.getElementById("a").innerHTML = JSON.stringify(myArr);
    return myArr;

}


function createSubArray(name){
    var arr = new Object();
    elems = document.getElementsByName(name);
    for (var i=0;i<elems.length;i++){
        if (elems[i].checked){
            arr[name] =  elems[i].value;
            arr['price'] =  elems[i].getAttribute('data-price');
        }
    }
    return arr;

}

And i got the output as below

array
  0 => 
    array
      'apple' => string 'light' (length=5)
      'price' => string '10' (length=2)
  1 => 
    array
      'apple' => string 'light1' (length=5)
      'price' => string '10' (length=2)
  2 => 
    array
      'Mango' => string 'dark' (length=4)
      'price' => string '40' (length=2)
  3 => 
    array
      'Pineapple' => string 'dark' (length=4)
      'price' => string '60' (length=2)
  4 => 
    array
      'Grape' => string 'dark' (length=4)
      'price' => string '80' (length=2)

But i need to array to be like below

array
  0 => 
    array
      'apple' => string 'light' (length=5)
       0 => 
           array
            'apple' => string 'light' (length=5)
            'price' => string '10' (length=2)
       1 => 
           array
            'apple' => string 'light1' (length=5)
            'price' => string '10' (length=2)
  1 => 
    array
      'Mango' => string 'light' (length=5)
       0 => 
           array
            'Mango' => string 'light' (length=5)
            'price' => string '10' (length=2)
  2 => 
    array
      'Pineapple' => string 'light' (length=5)
       0 => 
           array
            'Pineapple' => string 'light' (length=5)
            'price' => string '10' (length=2)

so i have changed the javascript like this

function createArray()
{
    var myArr = new Object();
    var _tempa = new Array();
    var elems = document.getElementsByTagName("input");
     for (var i=0;i<elems.length;i++)
     {  if (elems[i].checked){
         _tempa.push(elems[i].getAttribute('name'));
            myArr[i] = createSubArray1(_tempa);
          }
      }
     document.getElementById("a").innerHTML = JSON.stringify(myArr);
    return myArr;
} 

function createSubArray1(namearr){

    var arr1 = new Object();
    for (var j=0;i<namearr.length;j++){
    var elems = document.getElementsByName(name[j]);
    for (var i=0;i<elems.length;i++){
        if (elems[i].checked)
        {
        arr1[elems[i].getAttribute('data-gpname')] =  createSubArray(elems[i].getAttribute('name'));
        }
    }
    }
    return arr1;
}

 function createSubArray(name){
    var arr = new Object();
    var elems = document.getElementsByName(name);
    for (var i=0;i<elems.length;i++){
        if (elems[i].checked)
        {
        arr['productname'] =  elems[i].value;
        arr['price'] =  elems[i].getAttribute('data-price');
        }
    }
    return arr;
}

After this i am getting the out put like below

[{},{}]

My Html Is Below

<form method="post">
Apple
<input type="radio" onclick="createArray()" id="one" name="apple" data-gpname="apple" data-price="10" value="light"/> Light
<input type="radio" onclick="createArray()" id="two" name="apple" data-gpname="apple" data-price="20" value="dark" /> Dark
<input type="radio" onclick="createArray()" id="one1" name="apple1" data-gpname="apple"  data-price="120" value="light2"/> Light11
<input type="radio" onclick="createArray()" id="two1" name="apple1" data-gpname="apple" data-price="210" value="dark1" /> Dark22
<input type="text" id="appleqty" name="appleqty" value="" />
<br>
Mango
<input type="radio" onclick="createArray()" id="three" name="Mango" data-gpname="Mango"  data-price="30" value="light"/> Light
<input type="radio" onclick="createArray()" id="one" name="Mango" data-gpname="Mango" data-price="40" value="dark" /> Dark
<input type="text" id="Mangoqty" name="Mangoqty" value="" />
<br>
Pine Apple
<input type="radio" onclick="createArray()" id="four" name="Pineapple" data-gpname="Pineapple" data-price="50" value="light"/> Light
<input type="radio" onclick="createArray()" id="five" name="Pineapple" data-gpname="Pineapple" data-price="60" value="dark" /> Dark
<input type="text" id="Pineappleqty" name="Pineappleqty" value="" />
<br>
Grape
<input type="radio" onclick="createArray()" id="six" name="Grape" data-gpname="Grape" data-price="70" value="light"/> Light
<input type="radio" onclick="createArray()" id="seven" name="Grape" data-gpname="Grape" data-price="80" value="dark" /> Dark
<input type="text" id="Pineappleqty" name="Pineappleqty" value="" />

<textarea name="a" id="a" cols="50"></textarea>
<input type="submit" name="se" value="se" />
</form>

I have used php json_decode to get the array Structure

  • 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-06-11T20:18:36+00:00Added an answer on June 11, 2026 at 8:18 pm

    Hope this will help you. The output is same like you told here.

    The source of this page is here.

    I made a another sample based on your comments. Try this and let me know.

    HTML:

    <form method="post" id="myform">
    Apple
    <input type="radio" onclick="constructJSON(this.name)" id="one" name="apple" data-gpname="apple" data-price="10" value="light"/> Light
    <input type="radio" onclick="constructJSON(this.name)" id="two" name="apple" data-gpname="apple" data-price="20" value="dark" /> Dark
    <input type="radio" onclick="constructJSON(this.name)" id="one1" name="apple1" data-gpname="apple"  data-price="120" value="light2"/> Light11
    <input type="radio" onclick="constructJSON(this.name)" id="two1" name="apple1" data-gpname="apple" data-price="210" value="dark1" /> Dark22
    <input type="text" id="appleqty" name="appleqty" value="" />
    <br>
    Mango
    <input type="radio" onclick="constructJSON(this.name)" id="three" name="Mango" data-gpname="Mango"  data-price="30" value="light"/> Light
    <input type="radio" onclick="constructJSON(this.name)" id="one" name="Mango" data-gpname="Mango" data-price="40" value="dark" /> Dark
    <input type="text" id="Mangoqty" name="Mangoqty" value="" />
    <br>
    Pine Apple
    <input type="radio" onclick="constructJSON(this.name)" id="four" name="Pineapple" data-gpname="Pineapple" data-price="50" value="light"/> Light
    <input type="radio" onclick="constructJSON(this.name)" id="five" name="Pineapple" data-gpname="Pineapple" data-price="60" value="dark" /> Dark
    <input type="text" id="Pineappleqty" name="Pineappleqty" value="" />
    <br>
    Grape
    <input type="radio" onclick="constructJSON(this.name)" id="six" name="Grape" data-gpname="Grape" data-price="70" value="light"/> Light
    <input type="radio" onclick="constructJSON(this.name)" id="seven" name="Grape" data-gpname="Grape" data-price="80" value="dark" /> Dark
    <input type="text" id="Pineappleqty" name="Pineappleqty" value="" />
    
    <textarea name="a" id="a" cols="50"></textarea>
    <input type="submit" name="se" value="se" />
    
    </form>
    

    Script:

    var rgArray = [];
    function constructJSON(name)
    {
      rgArray.push(name);
      var myArray = {};
      var productData = {};
      productData['products'] = createArray();
      $("#a").html(JSON.stringify(productData));
    }
    
    function createArray(){
      var group = {};
      var product;
      var data;
      var hasValue = false;
      $rgArray = $(rgArray);
      $.each($rgArray, function(i, gp){
        product = {};
        var el = "input[data-gpname='"+gp+"']";
        $el = $(el);
        hasValue = false;
        $.each($el, function(i, item){
          
          if(item.checked){
              data = {};
              data.name = $(item).val();
              data.price = $(item).attr("data-price");
                
              hasValue = true;
              product[$(item).attr("name")] = data;
            }
        });
        if(hasValue) group[gp] = product;
      });
      return group;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a working JavaScript code below which dynamically creates JSON object using JSON.parse
I have a tool which generates some html+javascript code, it shows the code in
I have a code in Javascript as : _.each(this.collection.models, function (student) { $(this.el).append(new StudCloneItemView({
I have a website to which i've added dynamic javascript code generated on server.
I have JavaScript code which copies the value of input file and paste it
I have a JavaScript code which links to survey poll page. When I am
Hi Javascript gurus, I have this Javascript code which is working fine on Firefox
i have this javascript code to which i want to add the jquery fade-in
I have the following JavaScript code which I like to convert to jQuery but
I have a javascript code using which I want to accomplish the following .

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.