将数组值插入另一个数组Javascript的随机位置

我有两个数组:

var a = ["Saab", "Volvo", "BMW"]
vab b = ["Banana", "Mango", "Orange"]

我想将b数组值插入a的随机位置。预期输出应为:

var c = ["Banana", "Saab", "Volvo", "BMW", "Orange", "Mango"]

此处,将b数组值插入到a中,但不会妨碍a数组的值序列。如何使用Javascript做到这一点?

"Saab" → "Volvo" → "BMW"
评论
  • 吐泡泡oo
    吐泡泡oo 回复

    You can repeatedly pop values from b and splice them into a at random positions:

    while (b.length) {
        a.splice(Math.floor(Math.random() * (a.length + 1)), 0, b.pop());
    }