Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Object that contains an array of arrays, how to remove object so it's only an array of arrays

I have an object that contains an array of arrays. I would like to remove the object so it’s only an array of arrays. Is there a way in JavaScript to do this or am I out of luck? I’m using Puppeteer to scrape a page and return all the text after mapping through a set of elements. I’ve already written a function to seprate it into the arrays I want but Puppeteer returns it as an object which I don’t want.
The object it returns looks like this

UPDATE

Turns out it’s returning a promise which is an object.
So the result that I get when I log to the console is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Promise {
    [ [], [], []... ]
}

So I guess my new question is how do I get only the result of the promise.

data = {
  [
    [], [], []...
  ]
}; 

I would like it to look like

data = [
  [], [], []...
]

Puppeteer function

const getText = async (data) => {
    const browser = await puppeteer.launch({ headless: true })
    const page = await browser.newPage()
    await page.goto(data.URL, { waitUntil: 'networkidle2' });
    const options = await page.$$eval('table[class="data-table"] > tbody > tr > td', (options) =>
        options.map((option) => option.textContent)
    );    
    await browser.close();
    const finalData = createGroups(options, 228);
    // the puppeteer function returns each texContent as a single array, meaning the page 
    // I'm scraping returns 1140 arrays, I want each array in my final product to contain 5 
    // elements so I did 1140/5, that's how I got 228
    console.log(finalData);
}

createGroups function

const createGroups = async (arr, numGroups) => {
    const perGroup = Math.ceil(arr.length / numGroups);
    const finalArr = new Array(numGroups)
      .fill('')
      .map((_, i) => arr.slice(i * perGroup, (i + 1) * perGroup));
    return finalArr;
}

Or is there is a way to just extract that large array which contains the smaller arrays and put that into it’s own variable? The way to do it with arrays would be LargeArray[0] but I don’t know how to do that with objects.

Thanks

>Solution :

The reason that you are getting a Promise returned is that your createGroups is async (without a need to be), and the return type of an async function is always a Promise.

The best way is to just change it to not be async

const createGroups = (arr, numGroups) => { // rest of code

If for whatever reason, you don’t want to do this, you can just await it in your main method.

const finalData = await createGroups(options, 228);

In almost every circumstance I can think of, the only times you should use async are:

  • You’re using await somewhere in the method
  • You’re passing the function as a callback to another function, which expects the callback to return a Promise and you don’t want to create the Promise manually
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading