Can I Merge or Join two Arrays stored in different LocalStorage Variables

Storage 1 = Count
Storage 2 = Counts

I tried to combine or join Some of the lists of values from Count and Counts together using this:

Dim saves, u
Dim saves, i

saves=JSON.parse(localStorage.BrgDst)

For i=0 To UBound(saves)
If  saves[i].Count Then 
Numb = (saves[i].Count)
RFa =(saves[i].RiseFall)
currentRecord=i  

For u = UBound(saves) To 0 Step -1
If  saves[u].Counts Then
RFb =(saves[u].RiseFallX)
currentRecord=u

Dim saves(), datix

datix={PNB: Numb, MRa: RFa, MRb: RFb}

saves=JSON.parse(localStorage.BrgDst)
saves.push(datix)
localStorage.BrgDst=JSON.stringify(saves)

End If
    Next u 
        End If
           Next i

This is what I want

  1. RiseFall = -3.822 RiseFallX = 3.821

  2. RiseFall = -0.617 RiseFallX = 0.617

  3. RiseFall = 0.363 RiseFallX = -0.364

But this is what I get

  1. RiseFall = -3.822 RiseFallX = 3.821

  2. RiseFall = -3.822 RiseFallX = 0.617

  3. RiseFall = -3.822 RiseFallX = -0.364

  4. RiseFall = -0.617 RiseFallX = 3.821

  5. RiseFall = -0.617 RiseFallX = 0.617

  6. RiseFall = -0.617 RiseFallX = -0.364

  7. RiseFall = 0.363 RiseFallX = 3.821

  8. RiseFall = 0.363 RiseFallX = 0.617

  9. RiseFall = 0.363 RiseFallX = -0.364

Is this possible
Thanks
Will.

Solved (After lots of time, trial and error)

    Dim Ax=[]
    Dim Saves, i
    Dim Saves, u
       
    `Saves=JSON.parse(localStorage.BrgDst)`
       
    For i = UBound(Saves) To 0 Step -1
    If Saves[i].Counts Then
      
    Ax.push(Saves[i].RiseFallX)
      
    currentRecord=i 
      
       End If
     Next

    For u =0 To UBound(Saves)
    If Saves[u].Count Then
    Numc=(Saves[u].Count)
    RFb=(Saves[u].RiseFall)
    RFc=CDbl((Ax[u]))           ''Imported RF values
    currentRecord=u

       Dim Saves(), datv
       
       datv={CTP: Numc, Ra: RFb, Rh: RFc, Rt: Rfd} 

    Saves=JSON.parse(localStorage.BrgDst)
      Saves.push(datv)
       localStorage.BrgDst=JSON.stringify(Saves)   
     
     End If
       Next

Hi William,
Can you please explain what the 2 statements
Dim saves, i
Dim saves, u

mean? As far as I know you can
A) only declare a variable once - declaring a variable with the same name twice doesn‘t make sense to me. If it works, I would expect that „saves“ contains the value of „u“ after these 2 statements, and the first one is obsolete.
B) the syntax of the Dim statement allows to provide a simple value - are „i“ and „u“ these simple values? Then again, why assigning both to the same variable „saves“ ?

Kind regards
Thomas

Thanks for pointing that out Thomas, it seems I can Dim as many Saves as I like and everything works all the same anyway? (Just copy and pasting for speed) I’m an amateur hobbyist who codes for my own profession and if something works i’ll use it.

Thanks
Will.

Hi Will,
I‘ll test what really happens with 2 DIM statements for the same variable.
In other basic versions this would create an error message. If the system doesn‘t generate an error, the only alternative I can think of is that the 2nd statement just overwrites the first one - in other words, you might as well remove the first one, without any difference to the rest of the program.

But maybe more important: what are „i“ and „u“ ? Are they variables you have used somewhere else in your code? And what‘s the purpose of these DIM statements? They define a variable „saves“ but according to the language description a comma and a 2nd variable name are not allowed, so I really don‘t know what the „ ,i“ and „ ,u“ parts of the statements are supposed to do.
Kind regards
Thomas

Hi Thomas, I more than likely have used both u and i somewhere previously in the code (App is at 75 forms now). I was under the impression that u and i were being used in the for loops to count out the values stored in the LocalStorage array. Count is the numerical order of the values stored in RiseFall. and Counts is the numerical order for the values stored in RiseFallX.
Because I’m saving to another “Dim’ed” variable just for the iteration (Numc, RFb, RFc) and possible calculations within the loop “Saves” or u or i serve no real purpose to me apart from making the loop work and getting the values stored back into LocalStorage.

Hi William,
I ran a little test here.
I was wrong about the syntax of the DIM statement - you can actually define multiple variables in 1 Dim statement like you do, so

Dim saves, u

defines 2 variables, „saves“ and „u“ as you also found out.
If you define the same variable name again in another Dim statement (like you do with „saves“), AppStudio doesn‘t complain, but it only defines the same variable again. So there‘s still only one „saves“ variable. I had expected a message saying „duplicate variable declaration“ or similar.
Kind regards
Thomas

1 Like

Oh and 1 more thing: having a „Dim“ statement inside a loop is a bad idea - like you have in your 2nd loop.

1 Like

Ok Thomas, Thanks for your help and expertise, “noted” with regards to the Dim statement within a loop :smile:

Will.