js给几个随机数组使它遍历并且去重
- 生成随机数组
const randomArray = [];
const length = 10;
for (let i = 0; i < length; i++) {
randomArray.push(Math.floor(Math.random() * length));
}
- 遍历并去重
const uniqueArray = [];
randomArray.forEach((item) => {
if (!uniqueArray.includes(item)) {
uniqueArray.push(item);
}
});
console.log(uniqueArray);
原文地址: https://www.cveoy.top/t/topic/YY3 著作权归作者所有。请勿转载和采集!