這是我的代碼。我嘗試了幾天後創建一個對象數組,然後我將其存儲在本地存儲中。這是問題所在,我需要先從本地存儲獲取現有值。如何將它變成Angular 2中的對象數組?
然後我需要添加新的數據對象到現有的數組。然後我將它轉換成JSON,以便我可以將它存儲回本地存儲。
onRegisterSubmit(){
const user = {
a: this.a,
b: this.b,
c: this.c,
id: Date.now()
}
var abc = [];
var get = JSON.parse(localStorage.getItem('user'));
abc = [get];
abc.push(user);
localStorage.setItem('user', JSON.stringify(abc));
console.log(JSON.stringify(abc));
console.log(get);
}
我想要的JSON是這樣的對象的數組,
[{"hour":1,"minute":21,"ampm":"PM","repeatDays":[],"message":"","todayOrTomorrow":"Tomorrow","isRepeatMode":false,"isEnabled":false,"id":"1493797882440"},{"hour":1,"minute":24,"ampm":"PM","repeatDays":[],"message":"","todayOrTomorrow":"Tomorrow","isRepeatMode":false,"isEnabled":false,"id":"1493797896257"},{"hour":6,"minute":14,"ampm":"PM","repeatDays":[],"message":"","todayOrTomorrow":"Tomorrow","isRepeatMode":false,"isEnabled":false,"id":"1493815470408"}]
這是我的JSON。
[[[[[[[{"id":1493820594019},{"id":1493820606448}],{"id":1493820609111}],{"id":1493820610150}],{"id":1493820610553}],{"id":1493820610827}],{"id":1493820611015}],{"id":1493820612018}]
請help.Been嘗試幾天,任何幫助將不勝感激。謝謝!
*會發生什麼,而不是你想要的工作?* – nitind
我不明白你@nitind –
你得到什麼錯誤信息,對於初學者。 – nitind