Arrays JSON文件附加对象
我试图迭代几十个链接,并希望通过增加每个索引和获取下一个页码来保存所有结果Arrays JSON文件附加对象,arrays,json,node.js,object,for-loop,Arrays,Json,Node.js,Object,For Loop,我试图迭代几十个链接,并希望通过增加每个索引和获取下一个页码来保存所有结果 //dependencies const jsonfile = require('jsonfile') const _ = require('lodash') const utf8 = require('utf8') const fs = require('fs') //config const file = './data/home.json' const unique = [
//dependencies
const jsonfile = require('jsonfile')
const _ = require('lodash')
const utf8 = require('utf8')
const fs = require('fs')
//config
const file = './data/home.json'
const unique = [
{ suggest: 'København kommune', count: '2577' },
{ suggest: 'København K, 1000', count: '14' },
{ suggest: 'København K, 1100', count: '36' }
];
// begin iterations
_.forEach(unique, (data) => {
var totalPages = Math.ceil(data.count / 99); // get total page numbers for each suggestion
for ( var i=0; i < totalPages; i++){
/**
* Map Cities from our array of unique suggestions
* Fetch promises for each city
* @param i {i++} the number of pages we can iterate
* @param fetch {data.suggest} individual fetch calls
*/
fetch(`https://home.dk/umbraco/backoffice/home-api/Search?SortType=&SortOrder=&CurrentPageNumber=${i}&SearchResultsPerPage=99&q=${utf8.encode(data.suggest)}&EjendomstypeV1=&EjendomstypeRH=&EjendomstypeEL=&EjendomstypeVL=&EjendomstypeAA=&EjendomstypePL=&EjendomstypeFH=&EjendomstypeLO=&EjendomstypeHG=&EjendomstypeFG=&EjendomstypeNL=&Forretningnr=&ProjectNodeId=&PriceMin=&PriceMax=&EjerudgiftPrMdrMin=&EjerudgiftPrMdrMax=&BoligydelsePrMdrMin=&BoligydelsePrMdrMax=&BoligstoerrelseMin=&BoligstoerrelseMax=&GrundstoerrelseMin=&GrundstoerrelseMax=&VaerelserMin=&VaerelserMax=&Energimaerker%5B%5D=null&ByggaarMin=&ByggaarMax=&EtageMin=&EtageMax=&PlanMin=&PlanMax=&Aabenthus=&Foraeldrekoebsegnet=&Projektsalg=&BoligKanLejes=&MapLngSW=&MapLatSW=&MapLngNE=&MapLatNE=&UserLng=&UserLat=&SearchType=0`)
.then(response => response.json())
.then(validate => {
validate.searchResults.map(all=> {
var result = {
longitude: data.lng,
latitude: data.lat
}
jsonfile.writeFileSync(file, result, {spaces: 2, flag: 'a'})
// fs.appendFileSync(file, ',\n'); // my stupid way of adding commas at the end of each object....
}) // validate.searchResults.map
}) // then(validate)
} // for( i < totalPages )
}); // _.forEach(unique, (data) => {
问题是输出既不创建尾随逗号,也不包含在数组中。我正在使用标志:“a”方法在每个for-each循环上附加对象。。。。请帮忙
// bad output
{
"longitude": 12.5893247949521,
"latitude": 55.6768760704466
}
{
"longitude": 12.5821935238857,
"latitude": 55.6778938800944
}
{
"longitude": 12.5905159440853,
"latitude": 55.6803210250784
}
//prefered output
[ // nest all objects inside an array
{
"longitude": 12.5893247949521,
"latitude": 55.6768760704466
}, // <--- add comma - ,,,,,
{
"longitude": 12.5821935238857,
"latitude": 55.6778938800944
}, // <--- add comma - ,,,,,
{
"longitude": 12.5905159440853,
"latitude": 55.6803210250784
}
] // close array
我试图将所有内容都推送到一个数组中,但它到达了一个点,在这个点上,它有60.000多个结果被推到了里面,当它试图保存结果时,它有点冻结了系统
我想通过使用标志将每个迭代附加到文件中:方法或管道方法。。。希望有人有更好的方法将此类数据保存到JSON文件中
提前非常感谢 如果要在文件中存储数组,请先创建数组:
var result = [];
_.forEach(unique, (data) => {
result.push({
longitude: data.lng,
latitude: data.lat
});
});
jsonfile.writeFileSync(file, result, {spaces: 2, flag: 'a'})
根据其独特性,还有更优雅的方式。例如,如果它是一个数组:
var result = unique.map(data => ({
longitude: data.lng,
latitude: data.lat
}));
谢谢你,菲利克斯,很抱歉没有提供一个更好的例子。。。希望你能继续帮助我