Here is the minimal amount of javascript to reproduce the issue
var test = [];
test["11h30"] = "15h00"
test["18h30"] = "21h30"
console.log(test);
console.log(JSON.stringify(test)); // outputs []
The issue with the above is that, while javascript will be happy to let you late-bind new properties onto Array
, JSON.stringify()
will only attempt to serialize the actual elements in the array.
A minimal change to make the object an actual object, and JSON.stringify
works as expected:
var test = {}; // here is thre only change. new array ([]) becomes new object ({})
test["11h30"] = "15h00"
test["18h30"] = "21h30"
console.log(test);
console.log(JSON.stringify(test)); // outputs {"11h30":"15h00","18h30":"21h30"}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…