Generate collections using an even quantity of items with the use of a loop

Currently, I am in the process of developing a website scraper using Javascript (Express) for my personal needs.

The main goal of this script is to extract basic text data from an external source and then convert it into JSON objects. However, I want to organize these objects in pairs inside an array, which is where I require some assistance from fellow developers like you.

At the moment, the output consists of generic JSON objects based on the number of extracted items (typically ranging from 8-16):

{
  name: "John Doe",
  email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="afc5c0c7c181cbc0caefc5c0c7c181ccc0c2">[email protected]</a>,
  status: "active"
},

{
  name: "Jane Doe",
  email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="85efe4ebe0abe1eae0c5efe4ebe0abe6eae8">[email protected]</a>,
  status: "inactive"
},


{
  name: "Johnny Walker",
  email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="69030601070710471e0805020c1b291e0805020c1b470a0604">[email protected]</a>",
  status: "active"
},

{
  name: "Jimmy Glenfiddich",
  email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e48e8d89899dca8388818a828d80808d878ca48388818a80828d80808d878cca878b89">[email protected]</a>
  status: "active"
}

The desired outcome should resemble the following structure:

{

"pair-number": 1,

"pair:" [

  {
    name: "John Doe",
    email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4a20252224642e252f0a2025222464292527">[email protected]</a>,
    status: "active"
  },

  {
    name: "Jane Doe",
    email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c9a3a8a7ace7ada6ac89a3a8a7ace7aaa6a4">[email protected]</a>,
    status: "inactive"
  },
]
},

{

"pair-number": 2,

"pair:" [
  {
    name: "Johnny Walker",
    email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="355f5a5d5b5b4c1b4254595e5047754254595e50471b565a58">[email protected]</a>",
    status: "active"
  },

  {
    name: "Jimmy Glenfiddich",
    email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="462c2f2b2b3f68212a2328202f22222f252e06212a232822202f22222f252e6825292b">[email protected]</a>
    status: "active"
  }
]
}

This is how my `server.js` file looks like:

var express = require('express');
var request = require('request');
var cheerio = require('cheerio');
var app     = express();

var url = 'http://testurl.com;
var name, email, status;

app.get('/scrape', function(req, res) {

    request(url, function (error, response, html) {

        if (!error && response.statusCode == 200) {

            var $ = cheerio.load(html);

            data = {"name": name, "email": email, "status": status };       

            $('.scrape-class').filter(function() {

                var that = $(this);

                name = that.find('h5').text();
                email = that.find('.email').text(); 
                status = that.find('dl').children().first().text();

                data.name = name;
                data.email = email;
                data.status = status;

                console.log(data);
            });
        }
    });
});

app.listen(80, function () {
    console.log('Example app listening on port 80!')
})

Answer №1

Utilize the modulo operator (%) to determine if an iteration is odd or even based on the remainder of a division operation.

Consider this basic concept:

var arr = [1,2,3,4,5,6];
var results = [];
for (var ii = 0, nn = arr.length; ii < nn; ii++)
{
   if (ii % 2 == 0) //0 % 2 = 0; 1 % 2 = 1; 2 % 2 = 0; 3 % 2 = 1;
   {
      //Even iteration: add the current value to a new array in the results array
      results.push([arr[ii]]);
   }
   else
   {
      //Odd iteration: add the current value to the last array in the results array
      results[results.length - 1].push(arr[ii]);
   }
}

//final results should be: [[1,2],[3,4],[5,6]]

In your scenario, maintain an external iteration count outside the filter function for it to work accordingly.

I made changes to your code and included a new results array:

var express = require('express');
var request = require('request');
var cheerio = require('cheerio');
var app     = express();

var url = 'http://testurl.com';
var name, email, status;

app.get('/scrape', function(req, res) {

    request(url, function (error, response, html) {

        if (!error && response.statusCode == 200) {

            var $ = cheerio.load(html);

            var results = []; //store final results here

            var data = {"name": name, "email": email, "status": status };

            var ii = 0;

            $('.scrape-class').filter(function() {

                var that = $(this);

                name = that.find('h5').text();
                email = that.find('.email').text(); 
                status = that.find('dl').children().first().text();

                data.name = name;
                data.email = email;
                data.status = status;

                console.log(data);

                if (ii % 2 == 0) { //0 % 2 = 0; 1 % 2 = 1; 2 % 2 = 0; 3 % 2 = 1;
                    //occurs on iterations: 0, 2, 4, etc
                    results.push({"pair-number": results.length + 1, "pair": [data]});
                } else {
                    //occurs on iterations: 1, 3, 5, etc
                    results[results.length - 1]["pair"].push(data);
                }

                ii++;
            });

            console.log(results); //display collected data after crawling
        }
    });
});

app.listen(80, function () {
    console.log('Example app listening on port 80!')
})

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

Troubleshooting Error Message in Reactjs + Express Twitter API: "Authentication Failed"

I am attempting to develop a straightforward app that enables me to post tweets. Currently, I have React running on port 3000 and an express server.js running on port 5000. My server.js file includes the following code: app.post("/twitter/message", async( ...

What is the method for initiating a loop from index 1 rather than index 0 in an *ngFor loop within Angular?

Currently, I am retrieving data from a dummy API where the response starts from 1, but the index is starting from 0. Is there any way I can adjust the index loop to start from 1 instead of 0? Below is the HTML code for *ngFor: component.html < ...

Dealing with the "error" parameter in JSON in Angular and Ionic

I am currently working with TypeScript and Angular 5.0. I have defined the structure of my backend response in the following interface: export interface JSONResponse { error?: { code: number, message: string }; data?: {}; } The method in my ...

Assigning the click action to a newly added div

I'm struggling to grasp how to add a click function to an element inside the appended part of my code. Despite reviewing previous answers, I can't seem to apply them to my specific situation. Can someone offer assistance? What I'm aiming for ...

The animatedModal.js dialog box is causing the link to be unclickable, despite having a z-index of -9999

I am currently utilizing animatedModal.js for creating simple dialog boxes on my website. Everything is functioning perfectly, except for one issue - I am unable to click on the link to my logo at the top of the page because the modal with opacity:0; z-ind ...

When using Rails 6 with Turbolinks 5, the turbolinks:load event may trigger multiple times

I've encountered an issue in my recent Rails 6 app involving Select2 and Turbolinks. My goal is to have a Select2 field on the home page that allows users to select a user and view their profile. Here is how my js file is structured: document.addEven ...

"Exploring the process of utilizing React hooks to interact with a MongoDB Express server through POST and GET requests

I encountered an issue with retrieving data from my API endpoint through a GET request. While I can successfully send and perform updates or deletions using the postTodo() method, there seems to be an issue with fetching the data. To address this, I imple ...

The React state that is declared inside the render method cannot be accessed within the

I am facing an issue with my methods that fetch data and set the state asynchronously. The problem is, the render method gets called before the state is updated, causing it to not display the correct information. Although the code below works independentl ...

Utilizing schema in JSON with Spark technology

When attempting to read from a json file and specify a schema, I encountered an issue with mapping a number to a Double. Despite trying FloatType and IntType, the conversion failed. During schema inference, the customer id is identified as a String, but I ...

Modify the JSON file stored on the local server

I am currently working on a project that is being hosted on a local server. My main objective is to be able to edit and save a JSON file that will contain the configurations for this project. I have succeeded in reading the file and accessing it using axio ...

Solving yarn conflicts when managing multiple versions of a package

My software application contains a vulnerability related to a package that has different versions available (1.x, 2.x, 3.x). Since many other packages rely on this particular one as a dependency, updating each one individually is not a viable solution at t ...

"Exploring the process of looping through a JSON object following an asynchronous retrieval of JSON data using

I am facing an issue while trying to iterate through a JSON object in jQuery after fetching it asynchronously. I have a function called 'listFiles' that uses async to successfully retrieve a file list from a directory (dir) by calling an API endp ...

How can I connect a React component to a static HTML page?

Currently, I am working on a React + Redux application and have come across an issue. I am trying to create a link to a privacy.html page that is located in the root of the project alongside index.html (which is the React app). The problem I'm facing ...

Is it possible to retrieve a local variable from a JavaScript function and utilize it outside of its

I've been struggling for hours to access a local variable outside of a function, but I can't figure out where I went wrong. Here's the code: Here's my edited code: if (lastMsg.toUpperCase().indexOf("@TEST") > -1) { var myPy ...

AJAX: JSON response is not valid

When using ajax to fetch data from my JSON file located at http:xxx.myserver.com/myfolder/example.json $.ajax({ url:"http:xxx.myserver.com/myfolder/example.json", dataType:"json", success:function(data, status) { alert("success"+data+" ...

Error message: npm command not recognized while running commands within an Electron application

While developing an electron app, I utilize shell commands with child_process.exec. One of the commands I use is npm run start, which functions perfectly in a development environment. However, upon building the application for production, all npm commands ...

Docker - Issue with locating module in docker container log

I encountered an error in Docker Desktop while examining the logs of Docker containers. When I executed the following command in the VS Code terminal: docker compose up The error message displayed was: > <a href="/cdn-cgi/l/email-protection" class= ...

How can I retrieve a ref from a child component in Vue.js?

As a beginner to vuejs with limited JavaScript experience, I am using Vue3 in Laravel. I have a child component that exposes a ref on an input field like so: <input v-model="raw_input" ref="raw" @input="checkLen ...

Transferring ajax data to a variable on a Node.js server

I am currently facing an issue with an ajax get request that involves passing data from an input into a variable and using it in a query. The situation is being handled within index.handlebars page: <!DOCTYPE html> <html> <head> ...

Steps for showcasing an image while performing in-memory processing with AngularJS

As a newcomer to JS, I am seeking guidance on how to enhance my AngularJS app. The app features a page that stores data in-memory and allows users to filter this data, which is then displayed in an ng-grid table. Is there a way to show an image like a spin ...