0
var noOfChild=document.getElementById('btnInput').value;

            for(var i=1;i<=noOfChild;i++){
            createJSON();

            function createJSON() {
                alert('working')
                var jsonObj =[];
               var startId=1;
                $("span[id=childNo]").each(function() {
                    var idChildName='childName'+startId;
                    var childName=document.getElementById(idChildName).value;

                    var idDobChild='dobChild'+startId;
                    var dobChild=document.getElementById(idDobChild).value;

                    var idSchoolName='schoolName'+startId;
                    var schoolName=document.getElementById(idSchoolName).value;

                    var idClassSection='classSection'+startId;
                    var classSection=document.getElementById(idClassSection).value;
                    alert(childName);
                    var item = {};
                    item ["child_name"] = childName;
                    item ["child_DOB"] = dobChild;
                    item ["child_class"] = classSection;
                    item ["child_school_name"] = "KV_32";

                   // jsonObj.push("{'"+startId+"':['"+item+"']}");
                    jsonObj.push(item);
                    startId=startId+1;
                });
                var obj={
                    "1":jsonObj
                };
                console.log(obj);

I want object like this:

{"1":{"child_name": "ytfy", "child_DOB": "1993-06-18", "child_class": "d", "child_school_name": "KV_32"}}

But i am getting like this:

1:{child_name: "ytfy", child_DOB: "1993-06-18", child_class: "d", child_school_name: "KV_32"}
4
  • what problem are you having Commented Mar 15, 2018 at 13:40
  • Please add a working snippet. Commented Mar 15, 2018 at 13:40
  • sorry i am new to this plz ignore my flaws Commented Mar 15, 2018 at 13:45
  • i want to add object inside another object dynamically Commented Mar 15, 2018 at 13:46

3 Answers 3

1

This implementation in my opinion is a lot cleaner and nicer to see. The only thing you have to understand is that by calling objectResult["1"] you also create a field "1"

var object = {"childName":"foo", "childSurname":"foo"};
var objectResult = {}

objectResult["1"] = object;
console.log(objectResult);

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

1 Comment

just appending JSON.Stringify(objectResult) works fine now
1

It looks like you might be simply misreading console.log()

If you try this code:

var obj = {
  '1': {
    test: 'some data'
  }
}

console.log(obj)

It outputs 1: {test: "some data"} to the chrome dev tools console, however it sits below an expandable line that says Object, indicating that the line is in fact a child key of an object. So while the console shows this, the actual object does in fact look like:

{
  '1': {
    test: 'some data'
  }
}

2 Comments

thanks bro dont know why its not going to the server
Now its working.using JSON.Stringify(obj) will solve the problem
1

var foo = { "oh" : "yeah" };
var bar = { "no" : "way" };
bar["foo"] = foo;
console.log(bar);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.