Little problem with the right Object-format (JSON/Arrays)

Hello, i have a little problem :grin::sweat_smile:.
My issue is → i can’t figure out the right JSON/Array-format to get data saved into DATABASE.

Example:
I have created this Object-Array here…(called —> “myFilter”).

When i am trying to write the values into —> “field1” of the “List-Copy”-DATABASE, i get an ERROR…


How to modify “myFilter”, to get the right format, that the DATA can be written into -->“field1” of the “List-Copy”-Collection ?

This here is the code, which generates the array (myFilter)…

let myArray0 = ["field1", "field2", "field3", "field4", "field5", "field6", "field7", "field8", "field9", "field10"]
let myArray1 = []
let myArray2 = []

if($w('#cbgListCopy').value[0]!==undefined) {console.log("Value-0 = " + $w('#cbgListCopy').value[0])
      myArray1.push($w('#cbgListCopy').value[0])
      myArray2.push(copyDATA[$w('#cbgListCopy').value[0]])
      myFilter.push({[myArray0[0]]: myArray2[0]}) <--This should be the issue causing line.... WRONG-FORMAT ???
}

CopyDATA: (console-example)

$w(‘#cbgListCopy’) —> this is a CheckBoxGroup!

Whole console-log…

Post the URL (and how to see the problem) and I’ll take a look.

1 Like

Thanks Yisrael,

already found a solution, but it is still not the end-one…

export async function btnAddList_click(AUSWAHL) {
 let myArray0 = ["field1", "field2", "field3", "field4", "field5", "field6", "field7", "field8", "field9", "field10"]
 let myArray1 = []
 let myArray2 = []
    console.log("CopyData: ", copyDATA)
    console.log("Settings-Data = ", settingsDATA)
    console.log("Settings-Data = ", settingsDATA.items)
    console.log("Settings-Data = ", settingsDATA.items[1])
    
 //Prüfen auf Erstauswahl (Checkboxen)
 if (state===undefined) {
 //zeige Auswahlfenster...
            $w('#boxSelection').show('float')
            $w('#btnTitle').label = settingsDATA["setup1"]  
        }


//  var person = {firstName:"Donald", lastName:"Trump", age:50, country:"Bolivia"};
 else {console.log("Auswahl bereits getätigt.")
            console.log("CB-VALUE:", $w('#cbgListCopy').value)
 //---[1]----------------------------------------------------------
 if($w('#cbgListCopy').value[0]!==undefined) {console.log("Value-0 = " + $w('#cbgListCopy').value[0])
                myArray1.push($w('#cbgListCopy').value[0])
                myArray2.push(copyDATA[$w('#cbgListCopy').value[0]])
                console.log("myArray1= ", myArray1)
                console.log("myArray2= ", myArray2)
                myFilter = ({[myArray0[0]]: myArray2[0]})
                console.log("myFilter =", myFilter)
            }
 //---[2]----------------------------------------------------------
 if($w('#cbgListCopy').value[1]!==undefined) {console.log("Value-1 = " + $w('#cbgListCopy').value[1])
                myArray1.push($w('#cbgListCopy').value[1])
                myArray2.push(copyDATA[$w('#cbgListCopy').value[1]])
                console.log("myArray1= ", myArray1)
                console.log("myArray2= ", myArray2)

                myFilter = [{[myArray0[0]]: myArray2[0], [myArray0[1]]: myArray2[1]}]
                console.log("myFilter =", myFilter)
            }
 //---[3]----------------------------------------------------------
 if($w('#cbgListCopy').value[2]!==undefined) {console.log("Value-2 = " + $w('#cbgListCopy').value[2])
                myArray1.push($w('#cbgListCopy').value[2])
                myArray2.push(copyDATA[$w('#cbgListCopy').value[2]])
                console.log("myArray1= ", myArray1)
                console.log("myArray2= ", myArray2)

                myFilter = [{[myArray0[0]]: myArray2[0], [myArray0[1]]: myArray2[1], [myArray0[2]]: myArray2[2]}]
                console.log("myFilter =", myFilter)
            }
 //---[4]----------------------------------------------------------
 if($w('#cbgListCopy').value[3]!==undefined) {console.log("Value-3 = " + $w('#cbgListCopy').value[3])
                myArray1.push($w('#cbgListCopy').value[3])
                myArray2.push(copyDATA[$w('#cbgListCopy').value[3]])
                console.log("myArray1= ", myArray1)
                console.log("myArray2= ", myArray2)

                myFilter = [{[myArray0[0]]: myArray2[0], [myArray0[1]]: myArray2[1], [myArray0[2]]: myArray2[2], [myArray0[3]]: myArray2[3]}]
                console.log("myFilter =", myFilter)
            }

 
 else {console.log("Value-0 = undefined")}

 
            console.log("myFilter-JSON =", JSON.stringify(myFilter))

            local.setItem("copyData",  JSON.stringify(myFilter))


            $w('#btnAddList').enable()
 if ($w('#cbgListCopy').value.length>0) {$w('#btnShowList').enable()}
        }

}

Important part is this one…

//---[1]----------------------------------------------------------
 if($w('#cbgListCopy').value[0]!==undefined) {
               console.log("Value-0 = " + $w('#cbgListCopy').value[0])
                myArray1.push($w('#cbgListCopy').value[0])
                myArray2.push(copyDATA[$w('#cbgListCopy').value[0]])
                console.log("myArray1= ", myArray1)
                console.log("myArray2= ", myArray2)
                myFilter = ({[myArray0[0]]: myArray2[0]})
                console.log("myFilter =", myFilter)
            }
 //---[2]----------------------------------------------------------
 if($w('#cbgListCopy').value[1]!==undefined) {
                console.log("Value-1 = " + $w('#cbgListCopy').value[1])
                myArray1.push($w('#cbgListCopy').value[1])
                myArray2.push(copyDATA[$w('#cbgListCopy').value[1]])
                console.log("myArray1= ", myArray1)
                console.log("myArray2= ", myArray2)

                myFilter = [{[myArray0[0]]: myArray2[0], [myArray0[1]]: myArray2[1]}]
                console.log("myFilter =", myFilter)
            }
 //---[3]----------------------------------------------------------
 if($w('#cbgListCopy').value[2]!==undefined) {
                console.log("Value-2 = " + $w('#cbgListCopy').value[2])
                myArray1.push($w('#cbgListCopy').value[2])
                myArray2.push(copyDATA[$w('#cbgListCopy').value[2]])
                console.log("myArray1= ", myArray1)
                console.log("myArray2= ", myArray2)

                myFilter = [{[myArray0[0]]: myArray2[0], [myArray0[1]]: myArray2[1], [myArray0[2]]: myArray2[2]}]
                console.log("myFilter =", myFilter)
            }
 //---[4]----------------------------------------------------------
 if($w('#cbgListCopy').value[3]!==undefined) {
                console.log("Value-3 = " + $w('#cbgListCopy').value[3])
                myArray1.push($w('#cbgListCopy').value[3])
                myArray2.push(copyDATA[$w('#cbgListCopy').value[3]])
                console.log("myArray1= ", myArray1)
                console.log("myArray2= ", myArray2)

                myFilter = [{[myArray0[0]]: myArray2[0], [myArray0[1]]: myArray2[1], [myArray0[2]]: myArray2[2], [myArray0[3]]: myArray2[3]}]
                console.log("myFilter =", myFilter)
            }

 else {console.log("Value-0 = undefined")}

+++and+++

 local.setItem("copyData",  JSON.stringify(myFilter))

Second part of code…is the “back-parsing” of the JSON-String on another site.

You will find all given CONSOLE-LOGS on this site…
https://www.media-junkie.com/pflegeservice
connected page to this project —> https://www.media-junkie.com/list-copy

How it should work?
Selecting → rows from TABLE —> creating a new DB-entries —> view on new page.


Problem = Saving the new created and modified “ROW-DATA” back to the new DATABASE. I have trouble to create —> JSON —> {xxx: “yyy”, aaa: } inside an Array.

It works already —> but just for one line and also the CODE is really not the best one.

My aim:
It should be able to create multiple “JSON-DATA-ROWs” in an ARRAY, which then will be send via “LOCAL-STORAGE” to next page —> “NEW-LIST”.

You will find this code on the very bottom in my project.

Perhaps you have a much better solution for me?
The mentioned CODE-part is very clumpy and ugly (on my opinion) :sweat_smile:

The short-version —> I need an bulkSave, which should work from every of the given DATABASE-PRESETS and also be able to handle the CHOICE of DATA, which will be selected in the pop-up CheckBox-Group.

Ok. i could get it to work.

The Problem was here…

wrong format…

myFilter = [{[myArray0[0]]: myArray2[0], [myArray0[1]]: myArray2[1], [myArray0[2]]:}]

right format…

myFilter = {[myArray0[0]]: myArray2[0], [myArray0[1]]: myArray2[1], [myArray0[2]]:}

Now everythink is working like it should.

Thanks anyway Yisrael.

Hi Russian-dima

Small help to have less code

This is what you have:



if($w('#cbgListCopy').value[0]!==undefined) {
               console.log("Value-0 = " + $w('#cbgListCopy').value[0])
                myArray1.push($w('#cbgListCopy').value[0])
                myArray2.push(copyDATA[$w('#cbgListCopy').value[0]])
                console.log("myArray1= ", myArray1)
                console.log("myArray2= ", myArray2)
                myFilter = ({[myArray0[0]]: myArray2[0]})
                console.log("myFilter =", myFilter)
            }
 //---[2]----------------------------------------------------------
 if($w('#cbgListCopy').value[1]!==undefined) {
                console.log("Value-1 = " + $w('#cbgListCopy').value[1])
                myArray1.push($w('#cbgListCopy').value[1])
                myArray2.push(copyDATA[$w('#cbgListCopy').value[1]])
                console.log("myArray1= ", myArray1)
                console.log("myArray2= ", myArray2)

                myFilter = [{[myArray0[0]]: myArray2[0], [myArray0[1]]: myArray2[1]}]
                console.log("myFilter =", myFilter)
            }
 //---[3]----------------------------------------------------------
 if($w('#cbgListCopy').value[2]!==undefined) {
                console.log("Value-2 = " + $w('#cbgListCopy').value[2])
                myArray1.push($w('#cbgListCopy').value[2])
                myArray2.push(copyDATA[$w('#cbgListCopy').value[2]])
                console.log("myArray1= ", myArray1)
                console.log("myArray2= ", myArray2)

                myFilter = [{[myArray0[0]]: myArray2[0], [myArray0[1]]: myArray2[1], [myArray0[2]]: myArray2[2]}]
                console.log("myFilter =", myFilter)
            }
 //---[4]----------------------------------------------------------
 if($w('#cbgListCopy').value[3]!==undefined) {
                console.log("Value-3 = " + $w('#cbgListCopy').value[3])
                myArray1.push($w('#cbgListCopy').value[3])
                myArray2.push(copyDATA[$w('#cbgListCopy').value[3]])
                console.log("myArray1= ", myArray1)
                console.log("myArray2= ", myArray2)

                myFilter = [{[myArray0[0]]: myArray2[0], [myArray0[1]]: myArray2[1], [myArray0[2]]: myArray2[2], [myArray0[3]]: myArray2[3]}]
                console.log("myFilter =", myFilter)
            }

 else {console.log("Value-0 = undefined")}

You can change it to this

for (i = 0; i < 4; i++) { //4 becouse you used it a total of 4 times
 if($w('#cbgListCopy').value[i]!==undefined) {
               console.log("Value-0 = " + $w('#cbgListCopy').value[i])
                myArray1.push($w('#cbgListCopy').value[i])
                myArray2.push(copyDATA[$w('#cbgListCopy').value[i]])
                console.log("myArray1= ", myArray1)
                console.log("myArray2= ", myArray2)
                 switch(i){
                 case 0: myFilter = ({
                    [myArray0[0]]: myArray2[0]
                    })
                 case 1: myFilter = [{
                    [myArray0[0]]: myArray2[0],
                    [myArray0[1]]: myArray2[1]
                 }]
                 break;
                 case 2: myFilter = [{
                    [myArray0[0]]: myArray2[0],
                    [myArray0[1]]: myArray2[1],
                    [myArray0[2]]: myArray2[2]
                 }]
                 break;
                 case 3: myFilter = [{
                    [myArray0[0]]: myArray2[0],
                    [myArray0[1]]: myArray2[1],
                    [myArray0[2]]: myArray2[2],
                    [myArray0[3]]: myArray2[3]
                 }]
                 break;
                 
                 }
                 
                console.log("myFilter =", myFilter)
             } else {
                console.log("Value-0 = undefined")}
         }
 

Its less of the same code,
Also if you happen to get more then 4 you can easily change it later and add another case to it.

Kind regards,
Kristof.

2 Likes

Thanks man! A real Ninja! :zap:

Yes it has a much better structure, of course. I like this, my problem is always, that i forget about this option and use simple if-else-queries.

But i have some another end-solution, also not a bad one, i just did not show it here yet, but here it is…

export async function btnAddList_click(AUSWAHL) {
let myArray0 = ["field1", "field2", "field3", "field4", "field5", "field6", "field7", "field8", "field9", "field10"]
 let myArray1 = []
 let myArray2 = []
 
 if (state===undefined) {
             $w('#boxSelection').show('float')
            $w('#btnTitle').label = settingsDATA["setup1"]  
        }

 else {console.log("Auswahl bereits getätigt.")

 for (let index = 0; index < $w('#cbgListCopy').value.length; index++) {
 if($w('#cbgListCopy').value[index]!==undefined) {
                myArray1.push($w('#cbgListCopy').value[index])
                myArray2.push(copyDATA[$w('#cbgListCopy').value[index]])
 if(index=0){
                    myFilter = {[myArray0[0]]: myArray2[0]}
                }
 if(index=1){
                    myFilter = {[myArray0[0]]: myArray2[0], [myArray0[1]]: myArray2[1]}
                }
 if(index=2){
                    myFilter = {[myArray0[0]]: myArray2[0], [myArray0[1]]: myArray2[1], [myArray0[2]]: myArray2[2]}
                }
 if(index=3){
                    myFilter = {[myArray0[0]]: myArray2[0], [myArray0[1]]: myArray2[1], [myArray0[2]]: myArray2[2], [myArray0[3]]: myArray2[3]}
                }
 if(index=4){
                    myFilter = {[myArray0[0]]: myArray2[0], [myArray0[1]]: myArray2[1], [myArray0[2]]: myArray2[2], [myArray0[3]]: myArray2[3], [myArray0[4]]: myArray2[4]}
                }
 
 if(index=5){
                    myFilter = {[myArray0[0]]: myArray2[0], [myArray0[1]]: myArray2[1], [myArray0[2]]: myArray2[2], [myArray0[3]]: myArray2[3], [myArray0[4]]: myArray2[4], [myArray0[5]]: myArray2[5]}
                }
            }               
        }   
 else {console.log("Value-0 = undefined")}
 
        FILTER.push(myFilter)
        console.log("FILTER = ", FILTER)
        local.setItem("copyData",  JSON.stringify(FILTER))
        console.log("FILTER = ", FILTER)
        $w('#btnAddList').enable()
 
 if ($w('#cbgListCopy').value.length>0) {$w('#btnShowList').enable()}
    }
}

But, you are right, the case-break-option seems to be a little bit more comfortable.:sweat_smile:

Thanks for advise.

1 Like

Well i’m glad it helps a bit :wink:
There is some other thing that is verry usefull i discoverd a while ago.
u are using array1,array2,array3 etc as variable names.

U can access them in a loop aswel like this
var array1 = “Something”
var array2 = “Some more things”
var array3 = “Alot of things”

for ( let i = 0 ; i < 3 ; i ++ ) {
console.log( eva l(“array” + i.toString())

}
//Something
//Some more things
//Alot of things

helped me alot in a program i once made years ago in another programming language.

kind regards,
Kristof.

2 Likes

@volkaertskristof
Ok, thanks! I will try to use this technique in future.
I think i know what you want to tell me (more compression of my code?)

@russian-dima
Well its always nice to have a clean compressed code, but this whas just a nice need-to-know thingy, can be handy in the future :slight_smile:
I have searched far and wide a few years ago to find something like that when i needed it.
It might come in handy one day :slight_smile:

Understood.

1 Like