我正在使用Node.js,并且有一个jsonFile可以让我创建一个对象:
var dateFile = './Dictionnary/lastUpdate.json';
var dateObj = jsonfile.readFileSync(dateFile);
对象就是这个:
var dateObj = {
"l2D": "2018-07-10T08:45:03.000Z",
"CU": "2018-07-10T08:45:03.000Z",
"l2G": "2018-07-10T08:45:03.000Z",
"la1": "2018-07-10T08:45:03.000Z",
"la2": "2018-07-10T08:45:03.000Z",
"lru": "2018-07-10T08:45:03.000Z",
"nu3": "2018-07-10T08:45:03.000Z",
"DictionnaryTime": "2018-07-30T07:59:47.000Z"
}
我想用这样的脚本更新属性的值:
var citiesDictNames = ['CU', 'l2D', 'l2G', 'la1', 'la2', 'lru', 'nu3'];
for (i = 0; i < csvCitiesNames.length; i++) {
newDate = function(...);
var name = citiesDictNames[i];
dateObj.name = newDate;
jsonfile.writeFile(dateFile, dateObj, function(err) {
if (err) {
console.error(err);
} else {
console.log('LastModified Date updated');
}
});
}
但是结果是这个对象:
var dateObj = {
"l2D": "2018-07-10T08:45:03.000Z",
"CU": "2018-07-10T08:45:03.000Z",
"l2G": "2018-07-10T08:45:03.000Z",
"la1": "2018-07-10T08:45:03.000Z",
"la2": "2018-07-10T08:45:03.000Z",
"lru": "2018-07-10T08:45:03.000Z",
"nu3": "2018-07-10T08:45:03.000Z",
"DictionnaryTime": "2018-07-30T07:59:47.000Z",
"name": "2018-07-04T16:12:43.000Z",
}
当我将dateObj.name
替换为dateObj.l2D
或其他有效值时,但是当值在arrayList中时,我不会成功。