将 geojson 数据追加到已有的 .js 文件中并保存。
示例代码:
// 假设已有 file.js 文件,内容如下: var data = { type: "FeatureCollection", features: [ { type: "Feature", geometry: { type: "Point", coordinates: [102.0, 0.5] }, properties: { name: "My Point" } } ] };
// 新增数据 var newData = { type: "Feature", geometry: { type: "LineString", coordinates: [ [102.0, 0.0], [103.0, 1.0] ] }, properties: { name: "My Line" } };
// 追加数据到 file.js 中 data.features.push(newData);
// 将数据转换为格式化后的 JSON 字符串 var jsonString = JSON.stringify(data, null, 4);
// 将数据写入文件中 var fs = require('fs'); fs.writeFile('file.js', jsonString, function(err) { if (err) { console.error(err); } else { console.log('Data appended to file.js'); } });