120
2021-07-30 16:08:54 120阅读 0喜欢
function createGroups(arr, numGroups) {
  const perGroup = Math.ceil(arr.length / numGroups);
  return new Array(numGroups)
  .fill('')
  .map((_, i) => arr.slice(i * perGroup, (i + 1) * perGroup));
}
console.log(createGroups(['cat', 'dog', 'pig', 'frog'], 2));
// 输出结果 [["cat", "dog"], ["pig", "frog"]]

console.log(createGroups([1, 2, 3, 4, 5], 3));
// 输出结果 [[1, 2], [3, 4], [5]]
209
2019-12-04 17:01:06 209阅读 0喜欢

字符串转数组split() 函数和数组转字符串join() 函数实例

var string="abcdefg"
console.log(string.split(''));
// 输出结果:["a", "b", "c", "d", "e", "f", "g"]

var array=["a", "b", "c", "d", "e", "f", "g"];
console.log(array.join(','));
// 输出结果:a,b,c,d,e,f,g
783访问人次
Camila Waz 2022-01-22 18:22:10
2019-10-12 09:34:11 186阅读 0喜欢
2019-04-26 11:07:56 69阅读 0喜欢