/* 使用程序生成长度18的随机整数数组,范围为0到100,形如
8,0,21,16,22,27
将生成的数组处理成一个新数组,要求新数组格式如下,形如
[
[ "0," "8" ],
[ "16" ]
[ "21”,"22" , "27" ],...以此内推
]
*/
根据以上规律求解:
// 随机数包含最大值、最小值
function random(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
var arr = [];
for (var i = 1; i <= 18; i++) {
arr.unshift(random(0, 100));
}
arr.sort((a, b) => {
return a - b;
});
// console.log(arr);
var obj = {};
arr.forEach((value) => {
var key = Math.floor(value / 10)
if (!obj[key]) {
obj[key] = []
}
obj[key].push(value)
})
var newarr = [];
for (var k in obj) {
newarr.push(obj[k])
}
console.log(newarr);