让我们说我有这个数组:const numbers = [1,2,3,4,5];
此数组:const letters = ['A','B','C'];
我想将'字母'条目随机放入'数字'中。我不在乎'字母'的顺序,但是我希望'数字'保持顺序。目标是拥有这种数组:
const randomLettersInNumbers = [1, 'B', 2, 3, 'A', 4, 'C', 5];
我该如何实现?
答案 0 :(得分:1)
const numbers = [1,2,3,4,5];
const letters = ['A','B','C'];
let randomLettersInNumbers = numbers
letters.forEach((item) => {
let index = Math.floor(Math.random() * randomLettersInNumbers.length)
randomLettersInNumbers.splice(index, 0, item)
})