【问题标题】:How to rearrange the JSON object that Artoo.js generates?如何重新排列 Artoo.js 生成的 JSON 对象?
【发布时间】:2018-08-14 18:10:20
【问题描述】:

我有一个非常简单的文件,它应该从一个网页中抓取一些数据。在阅读之后,我将自己设置为 artoo、request 和cheerio,但现在我被卡住了。这是我目前的代码。

request('http://www.ciclopi.eu/frmLeStazioni.aspx?ID=144', function (error, response, html) {
    if (!error && response.statusCode == 200) {
      var $ = cheerio.load(html);
      artoo.bootstrap(cheerio);

      var scraper = $('span.Stazione, span.TableComune, span.Red').scrape({
        class: 'class', 
        content: 'text'
      });
      console.log(scraper);
   }
});

此代码解析为 scraper 变量作为结构如下的 json 对象:

[ { class: 'Stazione', content: 'Galleria Gerace' },
{ class: 'TableComune', content: 'Via Carlo Matteucci' },
{ class: 'Red', content: '7 bici libere5 posti disponibili' },
{ class: 'Stazione', content: 'C. Marchesi' },
{ class: 'TableComune', content: 'Via M. Valgimigli' },
{ class: 'Red', content: '2 bici libere10 posti disponibili' },
{ class: 'Stazione', content: 'CNR-Praticelli' },
{ class: 'TableComune', content: 'Via G. Moruzzi' },
{ class: 'Red', content: '7 bici libere7 posti disponibili' } ]

我需要将 scraper 对象重新排列成这样的

scraper = [
    {
        "Name": the content for class Stazione,        
        "Address": the content for class TableComune,
        "Bikes": the content for class Red
    },
    {
        "Name": the content for class Stazione,        
        "Address": the content for class TableComune,
        "Bikes": the content for class Red
    }
    ...
]

我真的一头雾水,希望我能解释一下自己..

【问题讨论】:

  • myObj 在你的帖子中是完全无效的实体,你能更准确地指定你想要的结果吗?
  • 这就是我希望我的对象最终的样子,它基本上将刮板对象中的“Stazione”、“TableComune”和“Red”分组。它们是自行车共享站的数据。我会尝试以更好的方式进行编辑。
  • 您的更新有所帮助,因此我能够解决您的问题。

标签: javascript arrays json sorting data-structures


【解决方案1】:

对于这种突变,我会使用Array.prototype.reduce 方法。

const data = [ 
  { class: 'Stazione', content: 'Galleria Gerace' },
  { class: 'TableComune', content: 'Via Carlo Matteucci' },
  { class: 'Red', content: '7 bici libere5 posti disponibili' },
  { class: 'Stazione', content: 'C. Marchesi' },
  { class: 'TableComune', content: 'Via M. Valgimigli' },
  { class: 'Red', content: '2 bici libere10 posti disponibili' },
  { class: 'Stazione', content: 'CNR-Praticelli' },
  { class: 'TableComune', content: 'Via G. Moruzzi' },
  { class: 'Red', content: '7 bici libere7 posti disponibili' }
];

const result = data.reduce((acc, item) => {
  if (item.class === 'Stazione') {
    acc.push({ 'Name': item.content });
  } else {
    const last = acc[acc.length - 1];
    if (item.class === 'TableComune') {
      last['Address'] = item.content;
    } else { // if (item.class === 'Red')
      last['Bikes'] = item.content;
    }
  }
  return acc;
}, []);

这里我们遍历初始数据数组并通过 2 个条件点生成结果数组:

  • 如果当前项目是 Stazione,那么我们将一个新对象推送到结果数组;此对象将具有与当前 Stazione 内容对应的 Name 属性
  • 否则我们将更新结果数组的最后一个元素:TableComune 内容将移至 Address 属性,Red 内容将移至 Bikes 属性。

该过程仅适用于初始数据数组中的 Stazione-TableComune-Red 对象的严格顺序。

【讨论】:

  • 就是这样!我一直在理解整个“推入”新对象是如何工作的,并且不知道 reduce 方法。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 2019-11-28
  • 1970-01-01
  • 1970-01-01
  • 2020-10-25
相关资源
最近更新 更多