javascript - 随机值 - 检查值 - 如果相同则取一个新值
问题描述
我正在为我的网站制作用户推荐。我在主页上显示 5 个用户推荐,@random
当用户加载网页时,这些推荐是从数组中提取的。在脚本随机获取 5 个推荐后,它会将其注入到我的 HTML 文件中。
这一切都很好,但我也希望检查每个值,以确保该值没有被其他 4 个推荐之一拉取。
然后,如果其中 2 个值相同,我希望脚本提取数组的另一个值。然后它再次需要检查提取的值是否未在其他推荐中使用。
我正在努力寻找一种尽可能高效的方法。如果我只提取 2 个值,我知道如何执行此操作,但现在我提取 5 个值,并且每个值都需要与提取的任何其他值进行检查。
var user = [
{
name: 'Tom Levis',
body: 'Toms text'
},
{
name: 'Some Random Guy',
body: 'Some guys text'
},
{
name: 'Peter',
body: 'Peters text'
}
];
var random1 = user[~~(Math.random() * user.length)];
var random2 = user[~~(Math.random() * user.length)];
var random3 = user[~~(Math.random() * user.length)];
var random4 = user[~~(Math.random() * user.length)];
var random5 = user[~~(Math.random() * user.length)];
document.getElementById('testimonial-1').innerHTML = random1.name + '<br>' + '<p id="testimonial-text">' + random1.body + '</p>';
document.getElementById('testimonial-2').innerHTML = random2.name + '<br>' + '<p id="testimonial-text">' + random2.body + '</p>';
document.getElementById('testimonial-3').innerHTML = random3.name + '<br>' + '<p id="testimonial-text">' + random3.body + '</p>';
document.getElementById('testimonial-4').innerHTML = random4.name + '<br>' + '<p id="testimonial-text">' + random4.body + '</p>';
document.getElementById('testimonial-5').innerHTML = random5.name + '<br>' + '<p id="testimonial-text">' + random5.body + '</p>';
最终应该发生的是从数组中提取了 5 个不同的值。所有结果都不应包含相同的值。
解决方案
我认为最有效的方法就是删除从可用选择池中选择的推荐。
const testimonials = [
{
name: 'Tom Levis',
body: 'Toms text'
},
{
name: 'Some Random Guy',
body: 'Some guys text'
},
{
name: 'Peter',
body: 'Peters text'
}
]
function getTestimonial() {
let index = ~~(Math.random() * testimonials.length)
let val = testimonials[index]
testimonials.splice(index, 1)
return val
}
var random1 = getTestimonial();
var random2 = getTestimonial();
var random3 = getTestimonial();
var random4 = getTestimonial();
var random5 = getTestimonial();
console.log(random1)
console.log(random2)
console.log(random3)
console.log(random4)
console.log(random5)
如果您想避免改变数据,您可以向每个条目添加属性,并根据已选择的推荐及其对应的数组id
检查随机推荐。id
id
const data = [
{
id: 1,
name: 'Tom Levis',
body: 'Toms text'
},
{
id: 2,
name: 'Some Random Guy',
body: 'Some guys text'
},
{
id: 3,
name: 'Peter',
body: 'Peters text'
}
]
const selected = []
function getTestimonials2(testimonials) {
while (true) {
let index = ~~(Math.random() * testimonials.length)
if (testimonials.length != selected.length) {
if (selected.indexOf(testimonials[index].id) === -1) {
selected.push(testimonials[index].id)
return testimonials[index]
}
} else {
return undefined
}
}
}
var random1 = getTestimonials2(data)
var random2 = getTestimonials2(data)
var random3 = getTestimonials2(data)
var random4 = getTestimonials2(data)
var random5 = getTestimonials2(data)
console.log(random1)
console.log(random2)
console.log(random3)
console.log(random4)
console.log(random5)
推荐阅读
- terraform - “value”:使用 aws_ssm_parameter 时无法设置此字段
- php - 如何在 Angular 6 中使用我的 php sendmail 函数
- jquery - 如何不在 JQuery 中单击?
- java - 运行开始前的线程状态不是New,是runnable,是不是错了
- google-chrome-extension - 更改函数只调用一次
- python - 如何根据模式一次转换多列的数据类型
- python - 创建一个新列的有效方法是由另一列分组的唯一值的总和?
- rgb - 如何将 RGB 输入转换为 YOLO v3 训练的灰度?
- c++ - 内部类 operator[] 重载不起作用
- python - 根据多个逻辑条件删除数值矩阵的行