Why I can't merge two arrays of objects into one? Error: array is not iterable

I’ve been working on a scraper. Now I’ve created two scrapers which are creating one array of objects each.

Now I’ve managed to scrape data, put them in seperate array, created component parseData where I’m sending these two arrays of objects inside it. And I now want to create a final one array of objects which will be sent to DB for inserting data.

Now here is where I’m trying to merge these two arrays:

const result = []
exports.parseData = (apartments) => {
  result.push(apartments);
  console.log(result);
};

And this doesn’t give me output that I expected. I expected two arrays to be merged not pushed into one. And I understand why is that happening – because of push method

Individual outputs of these two are:

apartments in olxScraper
[
   {
    id: '41676279',
    link: 'www.link.com',
    description: 'Description of apartment',
    price: 387000,
    picture: 'link to picture',
    squaremeters: 86,
    pricepersquaremeter: 4500
   },
   {
     //same object as before but different data
   }
]
apartments in santScraper
[
   {
    id: '3524',
    link: 'www.linkSecondScraper.com',
    description: 'Description',
    price: 150000,
    picture: 'picture',
    squaremeters: 55,
    pricepersquaremeter: 2727
   },
   {
     //same object as before but different data
   }
]

They are the same arrays with same key:value pairs in the objects. Only difference is number of objects inside of a array.

Now What would be proper way to merge these two? I just want one array that contains objects from these two arrays.

Also here is from where arrays are created:

const parsing = require('../data-functions/parseData');
const axios = require('axios');
const cheerio = require('cheerio');

exports.olxScraper = (count) => {
  const url = `https://www.olx.ba/pretraga?vrsta=samoprodaja&kategorija=23&sort_order=desc&kanton=9&sacijenom=sacijenom&stranica=${count}`;

  const apartments = [];
  const getRawData = async () => {
    try {
      await axios.get(url).then((res) => {
        const $ = cheerio.load(res.data);
        $('div[id="rezultatipretrage"] > div')
          .not('div[class="listitem artikal obicniArtikal  i index"]')
          .not('div[class="obicniArtikal"]')
          .each((index, element) => {
            $('span[class="prekrizenacijena"]').remove();
            const getLink = $(element)
              .find('div[class="naslov"] > a')
              .attr('href');
            const getDescription = $(element)
              .find('div[class="naslov"] > a > p')
              .text();
            const getPrice = $(element)
              .find('div[class="datum"] > span')
              .text()
              .replace(/\.| ?KM$/g, '')
              .replace(' ', '');
            const getPicture = $(element)
              .find('div[class="slika"] > img')
              .attr('src');
            apartments[index] = {
              id: getLink.substring(27, 35),
              link: getLink,
              description: getDescription,
              price: parseFloat(getPrice),
              picture: getPicture,
            };
          });

        const fetchSquaremeters = Promise.all(
          apartments.map((item) => {
            return axios.get(item.link).then((response) => {
              const $ = cheerio.load(response.data);
              const getSquaremeters = $('div[class="df2  "]')
                .first()
                .text()
                .replace('m2', '')
                .replace(',', '.')
                .split('-')[0];
              item.squaremeters = parseFloat(getSquaremeters);
              item.pricepersquaremeter = Math.round(
                parseFloat(item.price) / parseFloat(getSquaremeters)
              );
            });
          })
        );

        fetchSquaremeters.then(() => {
          //console.log(olxApartments);
          parsing.parseData(apartments);
        });
      });
    } catch (error) {
      console.log(error);
    }
  };
  getRawData();
};
const parsing = require('../data-functions/parseData');
const axios = require('axios');
const cheerio = require('cheerio');

exports.santScraper = (count) => {
  const url = `https://www.sant.ba/pretraga/prodaja-1/tip-2/cijena_min-20000/stranica-${count}`;

  const apartments= [];
  const getRawData = async () => {
    try {
      await axios.get(url).then((response) => {
        const $ = cheerio.load(response.data);

        $('div[class="col-xxs-12 col-xss-6 col-xs-6 col-sm-6 col-lg-4"]').each(
          (index, element) => {
            const getLink = $(element).find('a[class="re-image"]').attr('href');
            const getDescription = $(element).find('a[class="title"]').text();
            const getPrice = $(element)
              .find('div[class="prices"] > h3[class="price"]')
              .text()
              .replace(/\.| ?KM$/g, '')
              .replace(',', '.');
            const getPicture = $(element).find('img').attr('data-original');
            const getSquaremeters = $(element)
              .find('span[class="infoCount"]')
              .first()
              .text()
              .replace(',', '.')
              .split('m')[0];
            const pricepersquaremeter =
              parseFloat(getPrice) / parseFloat(getSquaremeters);

            apartments[index] = {
              id: getLink.substring(42, 46),
              link: getLink,
              description: getDescription,
              price: Math.round(getPrice),
              picture: getPicture,
              squaremeters: Math.round(getSquaremeters),
              pricepersquaremeter: Math.round(pricepersquaremeter),
            };
          }
        );
        parsing.parseData(apartments);
      });
    } catch (error) {
      console.log(console.log(error));
    }
  };
  getRawData();
};

15 thoughts on “Why I can't merge two arrays of objects into one? Error: array is not iterable”

  1. Your parseData function excepts 2 arguments.

    exports.parseData = (olxApartments, santApartments) => {
      const result = [...olxApartments, ...santApartments];
      console.log(result);
    };
    

    But in both cases where you call the function, you are only passing it one argument.

    parsing.parseData(olxApartments);
    
    parsing.parseData(santApartments);
    

    So you are getting this error because inside the parseData function, santApartments is undefined and therefore not iterable. Both times you are calling the function, you are only passing one argument to it, which is then referenced as olxApartments.

    parseData does not store the values of these arguments. If you want to combine olxApartments and santApartments, you need to create an array at a higher scope than this function, and push the relevant data to that array.

    Reply

Leave a Comment