Tips for reorganizing the JSON data created by Artoo.js?

My file aims to scrape data from a single webpage, but I've hit a roadblock. I initially tried using artoo, request, and cheerio based on my research. Here's the code I have so far:

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);
   }
});

This code returns the scraper variable as a json object with this structure:

[ { 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' } ]

I want to transform the scraper object into a format like this:

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

I'm struggling here and hoping for some guidance to rearrange the data as needed.

Answer №1

If I encounter mutations like these, my go-to method would be to utilize the Array.prototype.reduce method.

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;
}, []);

By iterating through the initial data array, we create a new result array based on two conditions:

  • If the current item is a Stazione, a new object with a Name property matching the current Stazione content is added to the result array.
  • Otherwise, the TableComune content moves to the Address property and the Red content moves to the Bikes property in the last element of the result array.

This process assumes a strict order of Stazione-TableComune-Red objects in the initial data array to work effectively.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What steps should be taken to resolve the error message "This Expression is not constructable"?

I'm trying to import a JavaScript class into TypeScript, but I keep getting the error message This expression is not constructable.. The TypeScript compiler also indicates that A does not have a constructor signature. Can anyone help me figure out how ...

Sending POST data to a PHP file without the use of jQuery results in AJAX malfunction

I have been struggling to make an ajax alert layer work with a POST method for the past few days, and I cannot figure out why it is not functioning. I have successfully used the same code structure to send form data through ajax using POST on other admin p ...

A guide to monitoring and managing errors in the react-admin dataProvider

Rollbar has been successfully integrated into my react-admin app to track uncaught errors. However, I've noticed that errors thrown by the dataProvider are not being sent to Rollbar. It seems like errors from the dataProvider are internally handled w ...

Tips for transforming a slice into an array reference

Is there a way to convert an &[u8] into an &[u8; 3] without copying the original array? I want the new array to reference the existing one. ...

unable to decode JSON into a structure

After receiving an attribute in JSON format from a REST service and capturing it with an invokeHTTP processor, I am looking to incorporate it into a JSON content flow using the JOLT processor. My existing content looks like this: { "id": 123, "use ...

Ways to modify the color of cells in a table generated from JSON

In developing an HTML table based on JSON data, I have created a university semester map that displays student information including their ID, year, term, and required courses for graduation. While the table is successfully created, I aim to customize the ...

AngularJS: Share event from the parent component exclusively with its child components

I am looking to establish a way to broadcast an event from a parent directive to a specific child, without all children in every instance of the "parent" directive receiving it when using scope.broadcast in the parent directive link function. Current Beha ...

Angular button will be disabled if the form control is empty

Is there a way to deactivate the search button when the text box is empty? I attempted to use the "disabled" attribute on the search button, but it didn't work. Here is my HTML code: <div class="col-md-5 pl-0"> <div class="in ...

JavaScript is not designed to run a second time

Currently, I have a script set up using $(document).ready(). It works perfectly upon initial loading. However, I also need this script to execute whenever the user changes an HTML select control. Ideally, upon page load, I want the filter and sort functio ...

Combine es6 imports from the identical module using an Eslint rule or plugin

Looking to consolidate my ES6 imports from a single module into one for my React project. For example: import { Title } from "@mantine/core"; import { Center } from "@mantine/core"; import { Divider } from "@mantine/core"; T ...

Extracting data from JSON that includes both arrays and objects

I am encountering a JSON parsing issue while trying to read transactions data from payfort. Despite my attempts to parse it into POJO, I keep getting a mismatch error. [ [ { "response_code": "04000", "card_holder_ ...

The hyperlink and criteria provided are accurate, however, the webpage remains stagnant

My login page is supposed to redirect to the contact confirmation page when I receive a 200 response from the server, but it's not working. Can you help me find my mistake? const [form, setForm] = useState({}); async function checkLogin() { tr ...

Communication between an iOS application and a server

As I am currently developing an iOS app that requires a remote interactive server, I need to send queries and post data to the server. What would be the best approach for this task - using REST, JSON, or SOAP? Are there any tutorials or documentation ava ...

Transform JSON-formatted NSURLRequest into a String in Swift

My process involves sending a JSON structured string via NSURLRequest, removing the urlscheme prefix, converting it to JSON, and then manipulating it as needed. urlscheme://{"Type":"photo","Name":"Photo13"} To convert NSURLRequest to a string, I used the ...

In the context of Express, how are "static" and "non-static" defined?

The Express documentation explains that the express.static() middleware is used to serve static files like images, CSS, and JavaScript. However, when serving a front-end React app using express.static("some_build_dir"), which includes JS files ...

Rails with Ajax: The destroy.js.erb file has been rendered successfully, but the specific div element identified by its id is not being successfully

Trying to implement a feature where users can click an unfollow button to end a friendship using ajax. However, the div does not disappear even though the console indicates that destroy.js.erb was rendered successfully. The expected outcome is for the reco ...

Transforming a complete date and time stamp into the format dd/mm/yyyy with no time component on the user end

After fetching this datetime value from my Web API 2 backend: 2015-12-11T09:15:49.403 The objective is to transform it, on the AngularJS front end, into: 11-12-2015 The time information should be excluded. How can this be achieved within an input field ...

Restart the calling process using NodeJS command

Is there a way to automatically restart the calling process in case certain events occur while querying a database? I want the process to start over if specific conditions are met. ...

Switching Unicode icon when element is clicked

My form has two inputs - one for text input and the other for submitting, like a button. I have added an icon to the submit button and want it to change when clicked. <input class="searchBtn" id="submit" name="submit" type="submit" value="&#xf002"& ...

Delving into the World of CSS

I have been working on changing the background image using this JavaScript code, but I am not sure what to reference in the CSS file. Despite reading through everything, my screen still remains blank. $(function() { var body = $('body'); var bac ...