85
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]]
H5页面 Camila Waz 2021-07-30 16:08:54
209

字符串转数组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
JavaScript H5页面 Camila Waz 2019-12-04 17:01:06
722访问人次
Camila Waz 2022-03-07 10:46:42
vue H5页面 Camila Waz 2019-10-12 09:34:11
php H5页面 Camila Waz 2019-04-26 11:07:56