Let some array be saved in a local storage:
let arr = [1, 2, 3, 4, 5];
localStorage.setItem('data', JSON.stringify(arr));
Suppose now we need to somehow modify this array, for example, add one more element to the end of it or change an existing one.
To solve the problem, we retrieve a string with an array stored in the storage, convert this string into an array, perform the necessary manipulations with this array, convert this array back into a string and write it back to the storage:
let json = localStorage.getItem('data');
let data = JSON.parse(json);
data.push(6);
data[0] = '!';
localStorage.setItem('data', JSON.stringify(data));
Given the following array with users:
let users = [
{
surn: 'surn1',
name: 'name1',
age: 31,
},
{
surn: 'surn2',
name: 'name2',
age: 32,
},
{
surn: 'surn',
name: 'name3',
age: 33,
},
];
Save it to a local storage. Then
make 3
inputs and a button.
Let the last name, first name and
age be entered into the inputs. By
clicking on the button, write the
new user to the end of the array
saved in the storage.