Trying to Scrape an Array

I’m trying to scrape the following:

 <tr>
        <td style="font-family:eurof;font-size:14px;padding-top:0px;padding-bottom:5px;"><a style="font-family:eurof;font-size:14px;" href="jewelry">JEWELRY</a> &nbsp;&gt;&nbsp; <a style="font-family:eurof;font-size:14px;" href="jewelry/anklet">ANKLET</a> &nbsp;&gt;&nbsp; <a style="font-family:eurof;font-size:14px;" href="jewelry/anklet/fashion">FASHION</a> &nbsp;&gt;&nbsp; <a style="font-family:eurof;font-size:14px;" href="jewelry/anklet/fashion/"></a>
		</td>
      </tr>

I use:


var categories = [];
const cheerio = require("cheerio");
const $ = await cheerio.load(content);
categories.push($('a[style="font-family:eurof;font-size:14px;"]').text());

console.log(categories);

The results of my console.log are:

[ ‘JEWELRYANKLETFASHION’ ]

I want to get

[ ‘JEWELRY’,‘ANKLET’,‘FASHION’ ]

Hello there,

I think something like this might work:

let aArray = $('a[style="font-family:eurof;font-size:14px;"]');
for (let a of aArray) {
  categories.push(a.text());
}

Hope this helps

Thanks for the suggestion, Sky020, but it didn’t work.

$('a[style="font-family:eurof;font-size:14px;"]').each(function() {
  categories.push($(this).text())
})
console.log(categories);

Note: You must use function keyword function in the callback. Arrow function () => will not work, as we need to use the this keyword.

Reference:

Thanks so much, husseyexplores. That worked!

You can use an arrow function, but then you have to use the element parameter. It doesn’t make much difference, except you can name the parameter, which might help document what element it is.

$('a[style="font-family:eurof;font-size:14px;"]').each((i, anchorElement) => {
  categories.push($(anchorElement).text());
});

console.log(categories);

Using a map would also make sense, as you want an array.

const categories = $('a[style="font-family:eurof;font-size:14px;"]')
  .map((i, anchorElement) => $(anchorElement).text())
  .get();

console.log(categories);

I need to put each element in a return statement that looks like this:
return { categories }

I tried
return { categories[0],categories[1],categories[2] }
but I get the error:
SyntaxError: Unexpected token ‘[’

What is the key/value structure of the object?

{ categories[0],categories[1],categories[2] }

You need a key and a value, that is unless categories[index] is a key-value pair, which it isn’t based on the code so far, it’s just a single string value.

{ categories[0]: 'some value', categories[1]: 'some value', categories[2]: 'some value' }

{ propName0: categories[0]: propName1: categories[1]: propName2: categories[2]: }

const categories = ['JEWELRY', 'ANKLET', 'FASHION'];

const arrayOfObjects = categories.map((category) => {
  return {
    [category]: 'some value',
  };
});
console.log(arrayOfObjects);

/*
[ { JEWELRY: 'some value' },
  { ANKLET: 'some value' },
  { FASHION: 'some value' } ]
*/


const singleObject = categories.reduce((object, category) => {
  object[category] = 'some value';
  return object;
}, {});
console.log(singleObject);

/* 
{ JEWELRY: 'some value',
  ANKLET: 'some value',
  FASHION: 'some value' }
*/

When I use:

return {arrayOfObjects}

I get the error:

UnhandledPromiseRejection Warning: ReferenceError: arrayOfObjects is not defined

Well most likely the arrayOfObjects is a) not defined or b) used out of scope.

I need more context, I don’t know how or where you are returning this. Also, what I posted are just some examples of data shape transformations, I still don’t really know what the data structure you are going for looks like.

I am returning an array of objects. I have other statements like this one:

main_img = $(‘img#main_img’).attr(‘src’);

and I return it like this:

return {main_img}

I meant an array of elements not an array of objects.

The output is a CSV file.

I tried declaring the singleObject and the object variables outside of the function and I still got the following error when I put it in my return statement:

UnhandledPromiseRejectionWarning: ReferenceError: singleObject is not defined

Thank you lasjorg for all your help.

I circumvented the error by putting the return statement right after the block so I didn’t get the error

ReferenceError: singleObject is not defined

return {singleObject};

The problem now is that I get the entire object in one cell of my spreadsheet like this:

{“JEWELRY”:“some value”,“ANKLET”:“some value”,“FASHION”:“some value”,"":“some value”}

I want each property of the object to have its own cell.

I finally got it to work with a method that I had tried before I moved the return statement. It didn’t work previously but it does now.

let category1 = categories[0];
let category2 = categories[1];
let category3 = categories[2];

return {category1, category2, category3};