Where do I have to push that I get the following format?

I would like to tinker arrays from which I can assemble the following JSON file, so far everything works, I just can’t get any further at this point:

    var T_nn_IN = document.getElementById('datatablenewname');
    var tabledata_newname_initialname = [];
    var tabledata_newname_newname = [];
    var tabledata_newname_rev = [];
    // var tabledata_newname_IN2 = [];

    $(T_nn_IN).find('> tbody > tr > td').each(function () {
        //tabledata_newname_IN1.push($(this).html())
        if($(this).hasClass("initialname") == true){
            tabledata_newname_initialname.push($(this).html())
        }
        if($(this).hasClass("newname") == true){
            tabledata_newname_newname.push($(this).html())
        }
        if($(this).hasClass("rev") == true){
            tabledata_newname_rev.push($(this).html())

            myJSONtable.push({"Satz.Ursprungsdateien":tabledata_newname_initialname})
            myJSONtable.push({"Satz.NeuerName":tabledata_newname_newname})
            myJSONtable.push({"Satz.Revision":tabledata_newname_rev})
        }

Here you can see how it should look after the function has been run:

{
    
    "Satz": [
        {
            "Ursprungsdateien": "placeholder1",
            "NeuerName": "placeholder1",
            "Revision": "05"
        },
        {
            "Ursprungsdateien": "placeholder2",
            "NeuerName": "placeholder2",
            "Revision": "04"
        },
        {
            "Ursprungsdateien": "placeholder3",
            "NeuerName": "placeholder3",
            "Revision": "02"
        },
        {
            "Ursprungsdateien": "placeholder4",
            "NeuerName": "placeholder4",
            "Revision": "02"
        }
    ]
}

And here you can See what I actually get jet:

Satz.Ursprungsdateien: (4) ["placeholder1", "placeholder2", "placeholder3", "placeholder4"]

Satz.NeuerName: (4) ["placeholder1", "placeholder2", "placeholder3", "placeholder4"]

Satz.Revision: (4) ["05", "04", "02", "02"]

Where or how do I have to push the data so that it looks like it should.

So in the end I want 4 arrays each filled with a Satz.Ursprungsdateien, a Satz.NeuerName and a Satz.Revision.

thank you in advance.

16 thoughts on “Where do I have to push that I get the following format?”

  1. Hi you are pushing object in wrong way, you need to create array then push object into that array

    Replace these lines

        myJSONtable.push({ "Satz.Ursprungsdateien": tabledata_newname_initialname })
        myJSONtable.push({ "Satz.NeuerName": tabledata_newname_newname })
        myJSONtable.push({ "Satz.Revision": tabledata_newname_rev })
    

    With this

        if(typeof myJSONtable.Satz == 'undefined'){
            myJSONtable.Satz = []
            myJSONtable.Satz.push({
                "Ursprungsdateien": tabledata_newname_initialname, 
                "NeuerName": tabledata_newname_newname, 
                "Revision": tabledata_newname_rev})
        }else{
            myJSONtable.Satz.push({
                "Ursprungsdateien": tabledata_newname_initialname,
                "NeuerName": tabledata_newname_newname,
                "Revision": tabledata_newname_rev
            })
        }
    

    Updated

    Replace this

    if($(this).hasClass("initialname") == true){
        tabledata_newname_initialname.push($(this).html())
    }
    if($(this).hasClass("newname") == true){
        tabledata_newname_newname.push($(this).html())
    }
    if($(this).hasClass("rev") == true){
        tabledata_newname_rev.push($(this).html())
    
        myJSONtable.push({"Satz.Ursprungsdateien":tabledata_newname_initialname})
        myJSONtable.push({"Satz.NeuerName":tabledata_newname_newname})
        myJSONtable.push({"Satz.Revision":tabledata_newname_rev})
    }
    

    with

    var myObj = {}
    if ($(this).hasClass("initialname") == true) {
        myObj['Ursprungsdateien'] = $(this).html()
    }
    if ($(this).hasClass("newname") == true) {
        myObj['NeuerName'] = $(this).html()
    }
    if ($(this).hasClass("rev") == true) {
        myObj['Revision'] = $(this).html()
    }
    
    if (typeof myJSONtable.Satz == 'undefined') {
        myJSONtable.Satz = []
        myJSONtable.push(myObj)
    }else{
        myJSONtable.push(myObj)
    }
    
    Reply

Leave a Comment