The element that is left out when dividing an array in the form of a page

In my project, I want to operate by making Array in Array. But there is a problem somewhere, it does not include the last or the first data in Array while dividing the Arrays. What could be the reason for this?

Database Used: MySQL (with NodeJS Package: MySQL2)

Code:

    var page_version = 2;
    let data = await con.promise().query(`SELECT * FROM News ORDER BY pid DESC`);
    var news = data[0];

    var result = news.reduce((resultArray, item, index) => {
      const chunkIndex = Math.floor(index/page_version)

      if (!resultArray[chunkIndex]) {
        resultArray[chunkIndex] = []
      }

      resultArray[chunkIndex].push(item)
      return resultArray;
    });

  console.log(result);

Input:

[
  {
    pid: 8,
    staff_id: '749662028083494994',
    defcon: '★★★★★',
    title: 'qwe',
    content: 'qweqw',
    creation_date: 2021-11-05T12:07:39.000Z
  },
  {
    pid: 7,
    staff_id: '749662028083494994',
    defcon: '★',
    title: 'Lollb',
    content: 'Xasdqwd',
    creation_date: 2021-11-05T12:06:24.000Z
  },
  {
    pid: 6,
    staff_id: '749662028083494994',
    defcon: '★★',
    title: 'Ttiel',
    content: 'Xxocaxxa',
    creation_date: 2021-11-05T12:05:39.000Z
  },
  {
    pid: 5,
    staff_id: '749662028083494994',
    defcon: '★★',
    title: 'qweqw',
    content: 'sada',
    creation_date: 2021-11-04T20:07:20.000Z
  },
  {
    pid: 4,
    staff_id: '749662028083494994',
    defcon: '★★★',
    title: 'asdasd',
    content: 'qwefas',
    creation_date: 2021-11-04T19:09:34.000Z
  },
  {
    pid: 3,
    staff_id: '749662028083494994',
    defcon: '★',
    title: 'qweqwedsa',
    content: 'asdasda',
    creation_date: 2021-11-04T19:09:28.000Z
  },
  {
    pid: 2,
    staff_id: '749662028083494994',
    defcon: '★★★★★',
    title: 'qwedqwe',
    content: 'qweqwe',
    creation_date: 2021-11-04T19:09:22.000Z
  },
  {
    pid: 1,
    staff_id: '749662028083494994',
    defcon: '★★★★★',
    title: 'Test.',
    content: 'A Big Test.',
    creation_date: 2021-11-04T19:08:28.000Z
  }
]

Output:

{
  '0': [
    {
      pid: 7,
      staff_id: '749662028083494994',
      defcon: '★',
      title: 'Lollb',
      content: 'Xasdqwd',
      creation_date: 2021-11-05T12:06:24.000Z
    }
  ],
  '1': [
    {
      pid: 6,
      staff_id: '749662028083494994',
      defcon: '★★',
      title: 'Ttiel',
      content: 'Xxocaxxa',
      creation_date: 2021-11-05T12:05:39.000Z
    },
    {
      pid: 5,
      staff_id: '749662028083494994',
      defcon: '★★',
      title: 'qweqw',
      content: 'sada',
      creation_date: 2021-11-04T20:07:20.000Z
    }
  ],
  '2': [
    {
      pid: 4,
      staff_id: '749662028083494994',
      defcon: '★★★',
      title: 'asdasd',
      content: 'qwefas',
      creation_date: 2021-11-04T19:09:34.000Z
    },
    {
      pid: 3,
      staff_id: '749662028083494994',
      defcon: '★',
      title: 'qweqwedsa',
      content: 'asdasda',
      creation_date: 2021-11-04T19:09:28.000Z
    }
  ],
  '3': [
    {
      pid: 2,
      staff_id: '749662028083494994',
      defcon: '★★★★★',
      title: 'qwedqwe',
      content: 'qweqwe',
      creation_date: 2021-11-04T19:09:22.000Z
    },
    {
      pid: 1,
      staff_id: '749662028083494994',
      defcon: '★★★★★',
      title: 'Test.',
      content: 'A Big Test.',
      creation_date: 2021-11-04T19:08:28.000Z
    }
  ],
  pid: 8,
  staff_id: '749662028083494994',
  defcon: '★★★★★',
  title: 'qwe',
  content: 'qweqw',
  creation_date: 2021-11-05T12:07:39.000Z
}

The bottom 8th element is the outermost element instead of being in the top Array.

>Solution :

You dont have a reducer init value, an empty array:

var result = news.reduce((resultArray, item, index) => {
  const chunkIndex = Math.floor(index/page_version)

  if (!resultArray[chunkIndex]) {
    resultArray[chunkIndex] = []
  }

  resultArray[chunkIndex].push(item)
  return resultArray;
}, []); // <- HERE 

Leave a Reply