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

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.