Is it possible to combine all session storage into one?
I have these three arrays which I pushing into storage because I will access them later.
The reason why I'm doing this is that the array I need is on a separated page. So I can't combine the array before pushing it into storage.
Here's what i have tried.
function FnOne() {
let arrayInFnOne = [
{
'id': 1,
'Choices': 'Cheeta'
},
{
'id': 2,
'Choices': 'Eagle'
},
{
'id': 3,
'Choices': 'Elephant'
},
{
'id': 4,
'Choices': 'Duck'
},
{
'id': 5,
'Choices': 'Deer'
}
]
let FNDataOne = sessionStorage.setItem('FNoneData', JSON.stringify(arrayInFnOne));
}
function FnTwo(){
let arrayInFnTwo = [
{
'id': 6,
'Choices': 'Eggplant'
},
{
'id': 7,
'Choices': 'Tomato'
},
{
'id': 8,
'Choices': 'Potato'
},
{
'id': 9,
'Choices': 'String beans'
},
{
'id': 10,
'Choices': 'Squash'
}
]
let FNDataTwo = sessionStorage.setItem('FNtwoData', JSON.stringify(arrayInFnTwo));
}
function FnThree(){
let arrayInFnThree = [
{
'id': 11,
'Choices': 'Paper'
},
{
'id': 12,
'Choices': 'Pencil'
},
{
'id': 13,
'Choices': 'Monitor'
},
{
'id': 14,
'Choices': 'Pentel Pen'
},
{
'id': 15,
'Choices': 'Wire'
}
]
let FNDataThree = sessionStorage.setItem('FNthreeData', JSON.stringify(arrayInFnThree));
}
document.querySelector('button').addEventListener('click', () => {
let finalData = sessionStorage.setItem('FinalDataAns', concat(FNDataOne,FNDataTwo,FNDataThree))
console.log(finalData);
})
<button>
Save
</button>
You can get these 3 arrays from session storage and then parse them to Javascript objects. After all, you can use those objects to concat into the final array.
function FnOne() {
let arrayInFnOne = [{
'id': 1,
'Choices': 'Cheeta'
},
{
'id': 2,
'Choices': 'Eagle'
},
{
'id': 3,
'Choices': 'Elephant'
},
{
'id': 4,
'Choices': 'Duck'
},
{
'id': 5,
'Choices': 'Deer'
}
]
//you cannot get returned value from `setItem`
//let FNDataOne = sessionStorage.setItem('FNoneData', JSON.stringify(arrayInFnOne));
sessionStorage.setItem('FNoneData', JSON.stringify(arrayInFnOne));
}
function FnTwo() {
let arrayInFnTwo = [{
'id': 6,
'Choices': 'Eggplant'
},
{
'id': 7,
'Choices': 'Tomato'
},
{
'id': 8,
'Choices': 'Potato'
},
{
'id': 9,
'Choices': 'String beans'
},
{
'id': 10,
'Choices': 'Squash'
}
]
//you cannot get returned value from `setItem`
//let FNDataTwo = sessionStorage.setItem('FNtwoData', JSON.stringify(arrayInFnTwo));
sessionStorage.setItem('FNtwoData', JSON.stringify(arrayInFnTwo));
}
function FnThree() {
let arrayInFnThree = [{
'id': 11,
'Choices': 'Paper'
},
{
'id': 12,
'Choices': 'Pencil'
},
{
'id': 13,
'Choices': 'Monitor'
},
{
'id': 14,
'Choices': 'Pentel Pen'
},
{
'id': 15,
'Choices': 'Wire'
}
]
//you cannot get returned value from `setItem`
//let FNDataThree = sessionStorage.setItem('FNthreeData', JSON.stringify(arrayInFnThree));
sessionStorage.setItem('FNthreeData', JSON.stringify(arrayInFnThree));
}
//for demo purpose
FnOne()
FnTwo()
FnThree()
document.querySelector('button').addEventListener('click', () => {
//use `getItem` to get string data in session storage
//and `JSON.parse` them
//finally, concat all arrays
const concatArray = [].concat(['FNoneData', 'FNtwoData', 'FNthreeData'].map(key => JSON.parse(sessionStorage.getItem(key))))
//stringify to store in session storage
const stringConcatArray = JSON.stringify(concatArray)
sessionStorage.setItem('FinalDataAns', stringConcatArray)
//get saved data from sessionStorage
const finalData = sessionStorage.getItem('FinalDataAns');
//have to parse back to Javascript object
console.log(JSON.parse(finalData));
})
<button>
Save
</button>