0

What is the way to export the contents of localStorage as a string and remove specific key-value pairs from it?

Namely, I need to preserve only those keys which values are like or dislike. Other keys should be removed.

In other words, the resulting string should be

"anne":"dislike","jane":"like","john":"like","mike":"dislike"
// the order doesn't matter
localStorage.clear();
localStorage.setItem('anne', 'dislike');
localStorage.setItem('jane', 'like');
localStorage.setItem('john', 'like');
localStorage.setItem('mike', 'dislike');
localStorage.setItem('abcd', 'foo');
localStorage.setItem('efgh', 'bar');

// this may be useful for something . . .
const stringified = JSON.stringify(localStorage);

Object.keys(localStorage).forEach(function(key) {
  const value = localStorage.getItem(key);
  if (value === 'like' || value === 'dislike') {
    // what should be here?
  }
});

I spent a day trying solutions from the following links:

But I still couldn't figure it out.

I don't want to change the original contents of local Storage. I just want to export it as a "clean" string.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user90726
  • 939
  • 1
  • 8
  • 28
  • 1
    Yeah, not, you don't want to stringify `localStorage`. Either use an object (e.g. `{ anne: 'dislike', jane: 'like', ... }` and store that, stringified, in localStorage, or iterate over `localStorage` using a `for (;;)` loop, using `localStorage.length` and `localStorage.key(index)` for iteration. – Heretic Monkey Apr 06 '21 at 21:46

5 Answers5

2
localStorage.clear()
localStorage.setItem('anne', 'dislike')
localStorage.setItem('jane', 'like')
localStorage.setItem('abcd', 'foo')
localStorage.setItem('efgh', 'bar')

function getStorage() {
  const data = {}
  let i = localStorage.length
  while (i--) {
    let key = localStorage.key(i)
    let value = localStorage.getItem(key)
    if (/^(like|dislike)$/i.test(value)) {
      data[key] = value
    }
  }
  return data
}

console.log(getStorage())
// {
//   anne: "dislike",
//   jane: "like"
// }
0

You should

// if values is not equal to string 'like' or string 'dislike'
if (value !== 'like' || value !== 'dislike') {
  // if not equal to those, remove the item with this key name (key)
  localStorage.removeItem(key);
}
0

Try using

localStorage.removeItem('name');
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Satwinder Singh
  • 229
  • 2
  • 9
0

localStorage saves data in a string format and you have to convert it to an object in order to retrieve it

const value = localStorage.getItem(key);
value = JSON.parse(value);
console.log("value",value); //to check current value of object in console

I think this could be useful

-1

localStorage.clear();
localStorage.setItem('anne', 'dislike');
localStorage.setItem('jane', 'like');
localStorage.setItem('john', 'like');
localStorage.setItem('mike', 'dislike');
localStorage.setItem('abcd', 'foo');
localStorage.setItem('efgh', 'bar');

// this may be useful for something . . .
const stringified = JSON.stringify(localStorage);


var res = {};

Object.keys(localStorage).forEach(function(key) {
  const value = localStorage.getItem(key);
  if (value === 'like' || value === 'dislike') {
    res[key] = value;
  }
});

console.log(res);
Fakt309
  • 821
  • 5
  • 14
  • Isn't this exactly what the OP already has in the question? – Heretic Monkey Apr 06 '21 at 21:47
  • this part `if (value !== 'like' || value !== 'dislike') {localStorage.removeItem(key);}` was changed – Fakt309 Apr 06 '21 at 21:48
  • That explanation should be in the answer. – Heretic Monkey Apr 06 '21 at 21:49
  • its okay without explanation – Fakt309 Apr 06 '21 at 21:49
  • @Fakt309 It may be an answer, but not a good one. – Unmitigated Apr 06 '21 at 21:52
  • @Fakt309 You should *always* add an explanation. You may think the code is so easy that it speaks for itself - but not everyone will see it that way. – mason Apr 06 '21 at 21:58
  • Thanks. (The answer was `-1`, I upvoted it to `0`.) There actually should be `&&` and not `||`, but there is also another issue. The solution removes the "bad" keys from the localStorage itself, while I don't want to change it. I just want to "export" it as a new string. The original localStorage should not be changed. – user90726 Apr 06 '21 at 22:00