Skip to content

Instantly share code, notes, and snippets.

@luislobo14rap
Last active December 8, 2019 05:49
Show Gist options
  • Save luislobo14rap/f0ca232c5ab18545dc1b7a5e4e6887f2 to your computer and use it in GitHub Desktop.
Save luislobo14rap/f0ca232c5ab18545dc1b7a5e4e6887f2 to your computer and use it in GitHub Desktop.
setArray.js
// arrayToPlain.js v1
function arrayToPlain(array_, columns = 2, heading){
// clone array
array__ = array_.join('!@#$%$#@!').split('!@#$%$#@!');
if(heading){
array__ = [...heading, ...array__];
};
let csv = [];
let column = 0;
let tempArray = [];
while(array__.length > 0){
if(column != columns){
column++;
tempArray.push(array__.shift());
}else{
csv.push(tempArray.join(' '));
column = 0;
tempArray = [];
};
};
if(tempArray.length > 0){
csv.push(tempArray.join(' '));
};
return csv;
};
// getArray.js v1
function getArray(key, value){
if( localStorage.getItem('array_' + key) == null ){
return null;
};
return localStorage.getItem('array_' + key).split('!@#$%$#@!');
};
// setArray.js v1
function setArray(key, value){
if(typeof value == 'string'){
value = value.split(', ');
};
if( localStorage.getItem('array_' + key) != null ){
let oldValue = localStorage.getItem('array_' + key).split('!@#$%$#@!');
for(let i = 0; i < value.length; i++){
oldValue.push(value[i]);
};
value = oldValue;
};
value = value.join('!@#$%$#@!');
localStorage.setItem('array_' + key, value);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment