我想像这样创建一个json:
{
"19/01/2018": ["10", "11", "12", "18"],
"20/01/2018": ["13", "14", "15"],
"21/01/2018": ["10", "14", "16", "17"],
"24/01/2018": ["10", "15", "17"],
"26/01/2018": ["10", "10", "13" ,"18"],
"20/02/2018": ["10", "10", "13" ,"18"]
}
目前,我有一个活动,每次点击它都会启动此功能。但是,永远不要创建一个新的行,它会删除相同的行。我不知道怎么做......
this.createJSON = (date, heure) => {
let json = {};
if (localStorage.getItem('test')) {
console.log('YEnA');
json = JSON.parse(localStorage.getItem('test'));
} else {
console.log('Empty');
json = {
date : '',
hour: []
};
}
if ($.inArray(heure, json.hour) === -1) {
console.log('Jsuis pas encore lo');
json.date = date;
json.date[date] += json.hour.push(heure);
}
localStorage.setItem('test', JSON.stringify(json));
};
需要帮助。
由于
答案 0 :(得分:4)
根据您的第一个JSON,我认为这应该有效:
this.createJSON = (date, heure) => {
let json = {};
if (localStorage.getItem('test')) {
console.log('YEnA');
json = JSON.parse(localStorage.getItem('test'));
}
if ($.inArray(heure, json.hour) === -1) {
console.log('Jsuis pas encore lo');
if (json[date] === undefined) {
json[date] = [ heure ];
} else {
json[date].push(heure);
}
}
localStorage.setItem('test', JSON.stringify(json));
};