//
// 数组分解方法一
export function chunkFirstArray(array, size) {
  const result = [];
  for (let i = 0; i < array.length; i += size) {
    result.push(array.slice(i, i + size));
  }
  return result;
}

// 数组分解方法二
function chunkSecondArray(array, size) {
  return Array.from(
    {
      length: Math.ceil(array.length / size)
    },
    (_, index) => array.slice(index * size, (index + 1) * size)
  );
}

// 数组分解总测试
function chunkArrayTest() {
  const unevenArr = [1, 2, 3, 4, 5, 6, 7];
  const unevenChunked = chunkFirstArray(unevenArr, 3);
  // 结果:
  // [[1, 2, 3],
  // [4, 5, 6],
  // [7]]

  //
  // 使用示例
  const arr = [1, 2, 3, 4, 5, 6, 7, 8];
  const chunked = chunkSecondArray(arr, 3);
  // 结果:
  // [[1, 2, 3],
  // [4, 5, 6],
  // [7, 8]]

  const letJsonObj = {
    productList: [
      {
        id: 1,
        name: "商品A",
        price: 29.9
      },
      {
        id: 2,
        name: "商品B",
        price: 39.9
      },
      {
        id: 3,
        name: "商品C",
        price: 19.9
      },
      {
        id: 4,
        name: "商品D",
        price: 49.9
      },
      {
        id: 5,
        name: "商品E",
        price: 59.9
      },
      {
        id: 6,
        name: "商品F",
        price: 69.9
      },
      {
        id: 7,
        name: "商品G",
        price: 79.9
      }
    ],
    pageSize: 3,
    expectedChunks: [
      [
        {
          id: 1,
          name: "商品A",
          price: 29.9
        },
        {
          id: 2,
          name: "商品B",
          price: 39.9
        },
        {
          id: 3,
          name: "商品C",
          price: 19.9
        }
      ],
      [
        {
          id: 4,
          name: "商品D",
          price: 49.9
        },
        {
          id: 5,
          name: "商品E",
          price: 59.9
        },
        {
          id: 6,
          name: "商品F",
          price: 69.9
        }
      ],
      [
        {
          id: 7,
          name: "商品G",
          price: 79.9
        }
      ]
    ]
  };
  const chunker = chunkSecondArray(letJsonObj.productList, letJsonObj.pageSize);
}
