1

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) 
1

2 Answers 2

3

Code:

var array = ["stack", "overflow"] array.map(btoa); 
Sign up to request clarification or add additional context in comments.

Comments

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)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.