Angular 6:Change localStorage 中的数组项

Angular 6:Change array item in localStorage

我想更改本地存储中的对象 属性。对象会是这样

appID: {"userid":0,"username":null,"password":null,"saltPassword":null,"kraPassword":null,"kraPasskey":null,"panNumber":"BTRGH8774L","dob":"20/05/1980","address1":"24, New Palasia","address2":"Near vvds","address3":"dvdd","city":"","state":"","pincode":452001,"firstname":"Ramesh","middlename":"","lastname":"Patidar","fathername":"dvdvd","mothername":"vdsddvdsvdsvdsd","maritalStatus":null,"gender":1,"aadharNumber":"741204513909","mobile":"9479897412","email":"dvdsv@bha.com","token":null,"customerId":1,"strDob":null,"ekycApplicationId":1,"stage":"UPLOAD DOCUMENT","dd":null,"mm":null,"yyyy":null,"updatedAt":"26/10/2018","filter":null,"country":null,"stageId":null,"companyName":null,"openLayout":true}

喜欢 只是想从数组中更改 "mothername" 的值并添加新值。

保存后无法编辑本地存储值,但可以将其取回,将其存储在对象中,编辑所需的 属性 对象,然后再次将其保存在本地存储中。

let item =JSON.parse(localStorage.getItem(key));
item['mothername']='New value';
localStorage.setItem(key, JSON.stringify(item));

不要忘记在保存为本地存储时将其字符串化仅在获取时保存字符串并在对象中解析它。

使用新展开运算符的一行方法:

localStorage.setItem('appID', 
    JSON.stringify(
    { ...JSON.parse(localStorage.getItem('appID')), 
      ...{ 'mothername': 'Mommy' }})
);