0

I want to encode my array contents with base64 if possible in javascript (and then decode later).

Example:

var array = ["stack", "overflow"]
// base64.encode(array)
fiji
  • 32
  • 10
  • 2
    a simple google search would have yielded this exact page: https://developer.mozilla.org/en-US/docs/Glossary/Base64 – Zachucks Aug 28 '20 at 13:50

2 Answers2

3

Code:

var array = ["stack", "overflow"]

array.map(btoa);
jjuniob
  • 71
  • 4
1

In order to use the well-known function btoa, you'll first have to convert your array to string, in such a way that you can reverse the operation. JSON would be the string format to go for.

So to encode do:

base64 = btoa(JSON.stringify(array))

To decode do:

JSON.parse(atob(base64))
trincot
  • 211,288
  • 25
  • 175
  • 211