Javascript 如何重新排列Artoo.js生成的JSON对象?

Javascript 如何重新排列Artoo.js生成的JSON对象?,javascript,arrays,json,sorting,data-structures,Javascript,Arrays,Json,Sorting,Data Structures,我有一个非常简单的文件,应该刮一个网页的一些数据。在四处阅读之后,我开始向阿尔图、请求和欢呼,但现在我被卡住了。这是我目前掌握的代码 request('http://www.ciclopi.eu/frmLeStazioni.aspx?ID=144', function (error, response, html) { if (!error && response.statusCode == 200) { var $ = cheerio.load(html);

我有一个非常简单的文件,应该刮一个网页的一些数据。在四处阅读之后,我开始向阿尔图、请求和欢呼,但现在我被卡住了。这是我目前掌握的代码

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
    }
    ...
]

我真的很茫然,希望我能解释我自己。

对于这种突变,我会使用这种方法

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

  • 如果当前项是statzione,那么我们正在将一个新对象推送到结果数组中;此对象将具有与当前Statzione内容对应的Name属性
  • 否则,我们将更新结果数组的最后一个元素:TableComune内容将移动到Address属性,红色内容将移动到Bikes属性

该过程仅在初始数据数组中的Statzione TableComune红色对象顺序严格的情况下才有效。

myObj
在您的帖子中是完全无效的实体,您能否更精确地指定您想要的结果?这就是我希望我的对象最终看起来的样子,它基本上将“Statzione”、“TableComune”分组和“红色”从刮刀对象。它们是自行车共享站的数据。我将尝试以更好的方式进行编辑。您的更新帮助了我,所以我能够解决您的问题。就是这样!我被困在理解整个“推动”到一个新对象是如何工作的,而不知道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;
}, []);