Reputation: 39
if (lastUserMessage === '/love') {
const love = [];
newlove = prompt("What do you love?", "Thing you love.");
love.push(newlove);
localStorage.setItem('love', JSON.stringify(love));
botMessage = 'Awesome.';
}
if (lastUserMessage === 'What do I. love?') {
var love2 = JSON.parse(localStorage.getItem("love"));
botMessage = love2;
}
Its suppose to store more than 1 object in the array, but I only get 1 value back.
Upvotes: 0
Views: 476
Reputation: 219047
Currently you always initialize your value to an empty array:
const love = [];
But it sounds like you expect it to retain the previously stored values in local storage. To do that, just initialize it to those values:
const love = JSON.parse(localStorage.getItem('love'));
As an extra step you might also do a null
check on the stored array, just in case this is the first time the code was run and nothing is stored yet. Untested, but I suspect something like this may work:
const love = JSON.parse(localStorage.getItem('love') || '[]');
The idea being that getItem
will return null
if the item doesn't exist, so in that case you'd pass '[]'
to JSON.parse
and start with an empty array.
Upvotes: 1