在js中 随机选择五个afc为false并且不重复的值,并且取出其中text的值 con_text: [{ text: '小魔女1', num: '69.00', alert: '12.3700', afc: false }, { text: '小魔女2', num: '69.00', alert: '12.3700', afc: false }, { text: '小魔女3', num: '69.00', alert: '12.3700', afc: false }, { text: '小魔女4', num: '69.00', alert: '12.3700', afc: false }, { text: '小魔女5', num: '69.00', alert: '12.3700', afc: false }, { text: '小魔女6', num: '69.00', alert: '12.3700', afc: false }, { text: '小魔女7', num: '69.00', alert: '12.3700', afc: false }, { text: '小魔女8', num: '69.00', alert: '12.3700', afc: false }, { text: '小魔女9', num: '69.00', alert: '12.3700', afc: false }, { text: '小魔女10', num: '69.00', alert: '12.3700', afc: false }],
时间: 2023-12-24 17:01:52 浏览: 27
你可以使用以下代码来实现在 JavaScript 中随机选择五个 `afc` 为 `false` 并且不重复的值,并且取出其中 `text` 的值:
```javascript
// 原始数据
const con_text = [
{ text: '小魔女1', num: '69.00', alert: '12.3700', afc: false },
{ text: '小魔女2', num: '69.00', alert: '12.3700', afc: false },
{ text: '小魔女3', num: '69.00', alert: '12.3700', afc: false },
{ text: '小魔女4', num: '69.00', alert: '12.3700', afc: false },
{ text: '小魔女5', num: '69.00', alert: '12.3700', afc: false },
{ text: '小魔女6', num: '69.00', alert: '12.3700', afc: false },
{ text: '小魔女7', num: '69.00', alert: '12.3700', afc: false },
{ text: '小魔女8', num: '69.00', alert: '12.3700', afc: false },
{ text: '小魔女9', num: '69.00', alert: '12.3700', afc: false },
{ text: '小魔女10', num: '69.00', alert: '12.3700', afc: false }
];
// 随机选择五个不重复的值
const randomValues = [];
while (randomValues.length < 5) {
const randomIndex = Math.floor(Math.random() * con_text.length);
const selectedItem = con_text[randomIndex];
if (selectedItem.afc === false && !randomValues.includes(selectedItem)) {
randomValues.push(selectedItem);
}
}
// 获取其中的 text 值
const selectedTextValues = randomValues.map(item => item.text);
console.log(selectedTextValues);
```
运行以上代码,你将得到一个包含五个随机选择的 `afc` 为 `false` 的 `text` 值的数组。