EN
JavaScript - how to store array of objects in local storage?
1
answers
0
points
How can I store an array of objects inside local storage?
Let's say I have the following array:
const users = [
{ id: 1, name: 'Ann' },
{ id: 2, name: 'Tom' },
{ id: 3, name: 'Kate' }
];
1 answer
0
points
The first thing you need to do is use JSON.stringify()
method to convert array of objects into string. Then using setItem()
method you can save it in the local storage.
// ONLINE-RUNNER:browser;
const users = [
{ id: 101, name: 'Ann' },
{ id: 102, name: 'Tom' },
{ id: 103, name: 'Kate' }
];
localStorage.setItem('users', JSON.stringify(users));
See also
References
0 comments
Add comment