gpt4 book ai didi

javascript - 如何在javascript中弹出数组中的元素?

转载 作者:行者123 更新时间:2023-11-28 13:09:02 27 4
gpt4 key购买 nike

如何从数组中弹出随机选择的内容?
var number = ["a", "b", "c"];
var random = number[Math.floor(Math.random()*number.length)];

最佳答案

使用拼接

var number = ["a", "b", "c"];
var random = Math.floor(Math.random()*number.length);

console.log(number[random], ' is chosen');
var taken = number.splice(random, 1);

console.log('after removed, ', number);
console.log('number taken, ', taken);

关于javascript - 如何在javascript中弹出数组中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44339829/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com