JSON文件追加对象

时间:2017-05-17 13:32:19

标签: 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 = [ 
                 { 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) => {

问题是输出不会创建尾随逗号,也不会包含在数组中。我正在使用旗帜:&#39; a&#39;为每个循环在每个上添加对象的方法....请帮助

// 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+结果推入内部的程度,当它试图保存结果时它有点冻结系统。

我想通过使用标志将每次迭代附加到文件中:&#34; a&#34;方法或管道方法...希望有人有更好的方法将这种数据保存到JSON文件中!

非常感谢你!

1 个答案:

答案 0 :(得分:1)

如果要在文件中存储数组,请先创建数组:

var result = [];
_.forEach(unique, (data) => {
  result.push({
      longitude:  data.lng, 
      latitude:   data.lat
  });
});
jsonfile.writeFileSync(file, result, {spaces: 2, flag: 'a'})

取决于unique是什么,有更优雅的方式。例如。如果它是一个数组:

var result = unique.map(data => ({
  longitude:  data.lng, 
  latitude:   data.lat
}));